[英]Postgresql SELECTing from JSON column
假設我正在使用PG 9.3,並且我有一個帶有json列“ meta_data”的發布表:
json列“ meta_data”的示例內容
{
"content": "this is a post body",
"comments": [
{
"user_id": 1,
"content": "hello"
},
{
"user_id": 2,
"content": "foo"
},
{
"user_id": 3,
"content": "bar"
}
]
}
如何從meta_data列的comments數組中找到user_id = 1的所有帖子?
我幾乎是肯定的,我實施不正確,但是可以嘗試一下
select *
from posts
where id in (
select id from (
select id,
json_array_elements(meta_data->'comments')->'user_id' as user_id
from posts
) x
where cast(user_id as varchar) = '1'
);
可能有一個像@>
這樣的數組運算符,它將消除對嵌套select語句的需要,但我現在似乎無法使其正常工作。
讓我知道這是否走上了正確的道路,我敢肯定,如果需要的話,我們可以弄清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.