Sql 如何将此mongo查询移植到postgres?
我有一个原子更新,它按属性中的最低值查找记录,增加该值,并在一个查询中返回结果 如果我在SQL Server中,我会在事务中使用DECLARE和SET,并执行多个查询,但这种postgres do语法对我来说是煎饼 我看到Sql 如何将此mongo查询移植到postgres?,sql,mongodb,postgresql,Sql,Mongodb,Postgresql,我有一个原子更新,它按属性中的最低值查找记录,增加该值,并在一个查询中返回结果 如果我在SQL Server中,我会在事务中使用DECLARE和SET,并执行多个查询,但这种postgres do语法对我来说是煎饼 我看到DO$$DECLARE r记录这是一个好的开始,但我可以设置或通过其他方式将单个记录分配给该变量吗 在蒙哥: this.findOneAndUpdate( { active: true }, { $inc: { userCount: 1 } }, { s
DO$$DECLARE r记录代码>这是一个好的开始,但我可以设置或通过其他方式将单个记录分配给该变量吗
在蒙哥:
this.findOneAndUpdate(
{ active: true },
{ $inc: { userCount: 1 } },
{ sort: { userCount: 1 } }, // grab the lowest count for update
cb
);
在SQL Server中,我会这样做:(伙计,已经有一段时间了…)
不需要do语句。。。将普通update语句与returning子句一起使用,例如:
update foos
set userCount = userCount + 1
where id = (
select id
from foos
where active = 1
order by userCount desc
limit 1
)
returning *
update foos
set userCount = userCount + 1
where id = (
select id
from foos
where active = 1
order by userCount desc
limit 1
)
returning *