Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何在PostgresJSONB查询中检查键的值为true_Sql_Json_Postgresql_Boolean - Fatal编程技术网

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;
;