如何检查Postgres中的json
字段是否具有特定元素?
我尝试使用json->>'属性'不为null
,但没有成功。
如何检查Postgres中的json
字段是否具有特定元素?
我尝试使用json->>'属性'不为null
,但没有成功。
使用->
:
where (json->'attribute') is not null
?
运算符仅适用于顶级属性。如果您想检查 JSON 结构较低处的路径是否存在,请按照 Roman 的答案执行 .. where column->'foo'->'bar'->'baz' is not null
。 - Ether.. WHERE column->'foo'->'bar'->'baz' IS NOT NULL
至少在 Postgres 9.4 中不起作用,您需要用 ( )
包围左侧部分,以使表达式起作用:.. WHERE (column->'foo'->'bar'->'baz') IS NOT NULL
。 - Mariano Ruiz?
运算符而不是->
运算符。 - Rocket Appliances->
时,它会检查your_column_name.attribute
的值是否存在,而?
则会检查attribute
是否存在。例如:obj.field IS NULL
将返回true
,但obj ? 'field'
将返回false
。 - Eugen Konkov如果你的列名是json类型,这个方法同样适用:
SELECT * FROM table_name WHERE your_column_name->>'attribute' is not NULL;
无法从标量中提取元素
。 - Chadwhere (myfield::json->'attribute') is not null
。 - maennelcolumn->'attribute'
会返回null。如果该路径存在但值为null,例如column = '{"attribute":null}'
,那么column->>'attribute'
也会返回null。 - Ether