是否可以在输出中检索 where 子句中的哪个谓词被评估为真?
Is it possible to retrieve in output which predicate in where clause is evaluated true?
是否有可能在一个 select 查询中有一个带有很多谓词的 where 子句,在输出中检索哪个谓词被评估为真,以及真正的元组?
例如,给定这个 table:
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
现在我有一个相关的。如果我已经有一个 table 倾向于包含这个子句评估,例如 '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
有什么方法可以 'temporary' 在 select 查询期间对该字段进行定价(check_1 = clause_1,check_2 = clause_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 进行类型化查询,我想获得对象 'Person'一次性评估 'check' 个值。
再次感谢大家!
类似于:
select name, age, hair_color, (age >= 25) AS Clause1, (hair_color like 'Blonde') AS Clause2
from person
where (age >= 25) or (hair_color like 'Blonde')
应该完成这项工作。
旁注:没有%
的like
很奇怪
您可以包装条件:
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
数据类型。
我不确定优化器在下推条件时有多聪明,所以这可能比原始查询慢。
是否有可能在一个 select 查询中有一个带有很多谓词的 where 子句,在输出中检索哪个谓词被评估为真,以及真正的元组?
例如,给定这个 table:
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
现在我有一个相关的。如果我已经有一个 table 倾向于包含这个子句评估,例如 '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
有什么方法可以 'temporary' 在 select 查询期间对该字段进行定价(check_1 = clause_1,check_2 = clause_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 进行类型化查询,我想获得对象 'Person'一次性评估 'check' 个值。
再次感谢大家!
类似于:
select name, age, hair_color, (age >= 25) AS Clause1, (hair_color like 'Blonde') AS Clause2
from person
where (age >= 25) or (hair_color like 'Blonde')
应该完成这项工作。
旁注:没有%
的like
很奇怪
您可以包装条件:
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
数据类型。
我不确定优化器在下推条件时有多聪明,所以这可能比原始查询慢。