有没有其他方法可以匹配来自 PostgreSQL(9.3 版)json 对象的 bool 值而不将其转换为字符串?
我的意思是: 该表在其 jsoncolumn 列中包含以下对象:
'{"path":"mypath", "exists": true}'
以下查询获取记录(注意 exists
值是通过 ->>>
作为文本获取的):
select * from thetable where jsoncolumn ->> 'exists' = 'true';
而这个不是:
select * from thetable where jsoncolumn -> 'exists' = true;
我想知道是否有更合适的方法来进行 bool 比较?
最佳答案
这里是验证 json(b) bool 值的所有有效组合:
-- This works only with jsonb, not with json because in Postgres json type is just a string.
SELECT $${ "exists": true }$$::jsonb -> 'exists' = 'true';
-[ RECORD 1 ]
?column? | t
-- All the following works with regular json as well with jsonb:
SELECT ( $${ "exists": true }$$::json ->> 'exists' )::boolean;
-[ RECORD 1 ]
bool | t
SELECT ( $${ "exists": true }$$::json ->> 'exists' )::boolean IS TRUE;
-[ RECORD 1 ]
?column? | t
SELECT ( $${ "exists": true }$$::json ->> 'exists' )::boolean = TRUE;
-[ RECORD 1 ]
?column? | t
关于json - 比较 PostgreSQL 9.3 json 对象中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39493110/