postgresql 可以在postgres中标记组条件吗?

vsdwdz23  于 2023-03-22  发布在  PostgreSQL
关注(0)|答案(2)|浏览(112)

示例伪SQL

SELECT *, condlabel FROM tbl WHERE cond1:(col1 = 1) OR cond2:(col2 = 2 and col3 = 4)

因此结果将具有附加列,这些列具有它们所满足的精确条件标签

hgc7kmma

hgc7kmma1#

正如AymDev评论的那样,完美的解决方案是在选择块中添加as别名的条件

nvbavucw

nvbavucw2#

不可以,你只能在SELECT子句和you cannot use those in a WHERE clause中标记(创建别名表达式),但是你可以使用子查询来实现这一点:

SELECT *
FROM (
   SELECT *, (col1 = 1) AS cond1, (col2 = 2 and col3 = 4) AS cond2
   FROM tbl
) AS tmp
WHERE cond1 OR cond2

或者只是重复它们:

SELECT *, (col1 = 1) AS cond1, (col2 = 2 and col3 = 4) AS cond2
FROM tbl
WHERE (col1 = 1) OR (col2 = 2 and col3 = 4)

相关问题