Postgres IF 表.x >表.y
是否可以在 Postgres 8+ 或通用 SQL 中执行以下伪代码?
SELECT table1.a, table1.b ... table1.x, (IF table1.x > table1.y RETURN table1.y ELSE return table1.x AS table1.y), table1.z...
Is it possible to do the following pseudo-code in Postgres 8+ or generic SQL?
SELECT table1.a, table1.b ... table1.x, (IF table1.x > table1.y RETURN table1.y ELSE return table1.x AS table1.y), table1.z...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在这种情况下,
LEAST
是一个很好的解决方案,但更通用的转换是CASE
语句LEAST
is a good solution in this case, but the more generic conversion would be theCASE
statement为什么不将子查询重写为
SELECT LEAST(table1.x, table1.y) AS lst
,然后在当前使用
table1 的任何地方使用
?lst
.x有关
LEAST
。Why not just re-write the subquery as
SELECT LEAST(table1.x, table1.y) AS lst
and then use
lst
everywhere you're currently usingtable1.x
?See the Postgres manual for a description of
LEAST
.