我可以选择匹配某个json值的记录where properties->>'foo' = 'bar',但是如果键'foo'尚未设置怎么办?我试过where properties->>'foo' IS NULL但是我收到了一个错误
No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "merchants".* FROM "merchants" WHERE (properties->>'foo' IS NULL)
Run Code Online (Sandbox Code Playgroud)
这是运营商优先问题.IS NULL绑定比绑定更紧密->>,因此您的代码被读取为properties ->> ('foo' IS NULL).添加括号 - (properties ->> 'foo') IS NULL.
regress=> SELECT '{"a":1}' ->> 'a';
?column?
----------
1
(1 row)
regress=> SELECT '{"a":1}' ->> 'b';
?column?
----------
(1 row)
regress=> SELECT '{"a":1}' ->> 'b' IS NULL;
ERROR: operator does not exist: unknown ->> boolean
LINE 1: SELECT '{"a":1}' ->> 'b' IS NULL;
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
regress=> SELECT ('{"a":1}' ->> 'b') IS NULL;
?column?
----------
t
(1 row)
Run Code Online (Sandbox Code Playgroud)