Sql 如何在PostgresJSONB查询中检查键的值为true
例如,我的表格是:Sql 如何在PostgresJSONB查询中检查键的值为true,sql,json,postgresql,boolean,Sql,Json,Postgresql,Boolean,例如,我的表格是: CREATE TABLE mytable ( id bigint NOT NULL, foo jsonb ); 它有一些价值: id | foo -----+------- 1 | "{'a':false,'b':true}" 2 | "{'a':true,'b':false}" 3 | NULL 我想知道如何检查一个键的值是否为true,我应该使用哪个操作符 我想要这样的东西,可以检查值: SELECT 1 FROM mytabl
CREATE TABLE mytable (
id bigint NOT NULL,
foo jsonb
);
它有一些价值:
id | foo
-----+-------
1 | "{'a':false,'b':true}"
2 | "{'a':true,'b':false}"
3 | NULL
我想知道如何检查一个键的值是否为true
,我应该使用哪个操作符
我想要这样的东西,可以检查值:
SELECT 1
FROM mytable
WHERE
id=2
AND
foo['a'] is true
;
语法
foo['a']
在Postgres中无效
如果要访问键的值,需要使用->
运算符
select *
from mytable
where id = 2
and foo ->> 'a' = 'true';
SELECT 1
FROM mytable
Where
id=2
AND
(foo ->> 'a')::boolean is true;
;