我有一张简化的表格,看起来像这样:
id | type | header | body
===========================================
1 | A | {type: A} | {content: "Hi"}
2 | A | {type: A} | {content: "Hello"}
3 | B | {type: B} | ["Hi","Hello"]
以下查询给我一个错误:
> select * from Table where header->>'type'='A' and body->>'content' like 'H%'
ERROR: cannot extract field from a non-object
这很公平,但是此查询也是如此:
> select * from (select * from Table where header->>'type'='A') where body->>'content' like 'H%'
而这些不包括:
> select * from Table where type='A' and body->>'content' like 'H%'
> select * from Table where header->>'type'='A' and body->>'content'='Hello'
对于这种特定情况,我有变通办法(“ like”谓词没有得到适当的优先权),但令我担心的是,即使在这种情况下,我显然也无法依靠括号来控制评估顺序,它更改了对数据应用的约束。有什么通用的方法可以做到这一点吗?
您应该可以通过以下命令强制执行评估订单case
:
select *
from Table
where (case when header->>'type'='A'
then (case when body->>'content' like 'H%' then 1 end)
end) = 1;
这是我建议仅在case
语句中放入语句的唯一时间where
。
您还可以使用CTE保证订单:
with t as (
select t.*
from table t
where header->>'type'='A'
)
select t.*
from t
where body->>'content' like 'H%';
但是,这导致了实现中间结果的额外开销。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句