如何在postgres jsonb查询中检查键的值是否为true

时间:2018-01-08 12:32:26

标签: 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 mytable
WHERE
id=2
AND
foo['a'] is true
;

2 个答案:

答案 0 :(得分:1)

语法MyStr FinalStr 003.10TT032.1 9.4R53.1 在Postgres中无效。

如果您想访问密钥的值,则需要使用foo['a']运算符as documented in the manual

->>

答案 1 :(得分:0)

SELECT 1
FROM mytable
Where
id=2
AND
(foo ->> 'a')::boolean is true;
;