2017-01-20 7 views
0

jsonbdetailsの次の行が指定されています。キー名の末尾が_colで、値がBのレコードが選択されるようにクエリを作成するにはどうすればよいですか。したがって、ids 1,2のレコード。 id | details 1 | { "one_col": "A", "two_col": "B" } 2 | { "three_col": "B" } 3 | { another: "B" } キーが名前とキー値で終了するJSONB列が一致するクエリ行は、特定の値です

これまでのところ、キーではなく値に基づいて一致する方法しか見つけられませんでした。

答えて

1

使用ペア(key, value)としてJSONオブジェクトを与える関数jsonb_each_text()

with the_data(id, details) as (
values 
(1, '{ "one_col": "A", "two_col": "B" }'::jsonb), 
(2, '{ "three_col": "B" }'), 
(3, '{ "another": "B" }') 
) 

select t.* 
from the_data t, 
lateral jsonb_each_text(details) 
where key like '%_col' 
and value = 'B'; 

id |    details    
----+---------------------------------- 
    1 | {"one_col": "A", "two_col": "B"} 
    2 | {"three_col": "B"} 
(2 rows) 
+0

はどうもありがとうございました! –

関連する問題