我有一个列:
abs(triad.census(gr))
第一层对象可以包含各种类型和任意数量的类型。我的目标(用于自动化测试,而非生产,因此效率低下)是找到任何[
{
"type":"menu",
"prompt": {
"type":"say",
"message":"This is a menu. Pick option 1 or 2."
}
}
]
的记录。
基于https://stackoverflow.com/a/22687653/197606我知道我可以提供数字索引,但运行查询column.*.type = menu
会引发错误:
SELECT * FROM my_table WHERE 'my_column'->0->'type' = 'menu'
虽然指定ERROR: operator is not unique: unknown -> integer
LINE 1: SELECT * FROM my_table WHERE 'my_column'->0->'type' =...
^
HINT: Could not choose a best candidate operator. You might need to add explicit type casts.
Query failed
PostgreSQL said: operator is not unique: unknown -> integer
Hint: Could not choose a best candidate operator. You might need to add explicit type casts.
的数字索引适用于此特定用例并且是可接受的答案,但我更喜欢一种对列索引不挑剔的方法。
答案 0 :(得分:1)
您可以使用:
SELECT * FROM my_table WHERE my_column->0->>'type' = 'menu'
答案 1 :(得分:0)
PostgreSQL不允许查看json数组。您的解决方案可能是这样的:
SELECT *
FROM my_table
WHERE EXISTS (
SELECT 1
FROM jsonb_array_elements(my_column) AS j
WHERE j->>'type' = 'menu'
)