在带有where子句的select查询中是否有可能,其中where子句在输出中检索到很多谓词,而其中的一个谓词被评估为true以及真正的元组?
例如,给定此表:
Table person
name age hair_color
Tom 12 Brown
Bob 27 Black
Sam 20 Red
Ann 15 Blonde
John 30 Blonde
该查询:
select *
from person
where (age >= 25) or (hair_color = 'Blonde')
我想在输出中有这样的内容:
name age hair_color clause_1 clause_2
Bob 27 Black true false
Ann 15 Blonde false true
John 30 Blonde true true
您建议如何取得类似的结果?
更新
谢谢!你回答了我的问题!所以可能是这样的:
select *
from (
select p.*,
(age >= 25) as clause_1,
(hair_color = 'Blonde') as clause_2
from test.person as p
) as t
where t.clause_1 or t.clause_2
现在我有一个相关的。如果我已经有一个倾向于包含此子句评估的表,例如'check_1'和'check_2'。
Table person
name age hair_color check_1 check_2
Tom 12 Brown
Bob 27 Black
Sam 20 Red
Ann 15 Blonde
John 30 Blonde
在选择查询期间,是否有任何方法可以“临时”评估此字段的价值(check_1 =子句_1,check_2 =子句_2)?
name age hair_color check_1 check_2
Bob 27 Black true false
Ann 15 Blonde false true
John 30 Blonde true true
我问你,因为我在一个Java项目中需要它,在这个项目中我使用JPA和Criteria API进行类型化查询,并且我希望一次性获得具有“检查”值的对象“人”。
再次感谢大家!
您可以包装条件:
select *
from (
select p.*,
(age >= 25) as condition_1,
(hair_color = 'Blonde') as condition_2
from person p
) t
where condition_1 or condition_2
以上是标准SQL,并假设所使用的DBMS支持适当的boolean
数据类型。
我不确定优化器将条件降低的聪明程度,因此它可能比原始查询要慢。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句