我有一个订单表,我将订单摘要存储在jsonb
列
{"users": [
{"food": [{"name": "dinner", "price": "100"}], "room": "2", "user": "bob"},
{"room": "3", "user": "foo"}
]}
现在,我想使用users
查询所有food->name
。
我尝试了以下操作,但这也为用户提供了 foo ,没有食物。
select
jsonb_array_elements(jsonb_array_elements(summary->'users')->'food')->>'name' as food,
jsonb_array_elements(summary->'users')->>'user' as user_name
from orders;
food | user_name
-------+-----------
dinner | bob
dinner | foo
我该怎么做这样的查询?
更新
我还有两个食物选择这样的夏季
{"users": [
{"food": [{"name": "dinner", "price": "100"}, {"name": "breakfast", "price": "100"}], "room": "2", "user": "bob"},
{"room": "3", "user": "foo"}
]}
而不是我:
food | user_name
-----------+-----------
dinner | bob
breakfast | foo
理想情况下我想得到
food | user_name
----------------------+-----------
dinner, breakfast | bob
答案 0 :(得分:6)
好的,如果你这样做
SELECT jsonb_array_elements(summary->'users') as users FROM orders;
你得到了
┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
│ users │
├──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
│ {"food": [{"name": "dinner", "price": "100"}, {"name": "breakfast", "price": "50"}], "room": "2", "user": "bob"} │
│ {"room": "3", "user": "foo"} │
└──────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
让我们把这个选择放在另一个选择中,选择我们需要的东西:
SELECT users->'user' as user_name, users->'food'->0->'name' as food FROM (
SELECT jsonb_array_elements(summary->'users') as users FROM orders
) as s;
┌───────────┬──────────┐
│ user_name │ food │
├───────────┼──────────┤
│ "bob" │ "dinner" │
│ "foo" │ (null) │
└───────────┴──────────┘
我们关闭了。我们只需要添加WHERE
。
SELECT users->'user' as user_name, users->'food'->0->'name' as food FROM (
SELECT jsonb_array_elements(summary->'users') as users FROM orders
) as s WHERE (users->'food') is not null;
导致
┌───────────┬──────────┐
│ user_name │ food │
├───────────┼──────────┤
│ "bob" │ "dinner" │
└───────────┴──────────┘
如果食物阵列中有更多数据,如
'{"users": [{"food": [{"name": "dinner", "price": "100"}, {"name" : "breakfast", "price" : "50"}], "room": "2", "user": "bob"}, {"room": "3", "user": "foo"}]}'
你可以做到
SELECT users->'user' as user_name, jsonb_array_elements(users->'food')->>'name' as food FROM (
SELECT jsonb_array_elements(summary->'users') as users FROM orders
) as s WHERE (users->'food') is not null;
和
┌───────────┬───────────┐
│ user_name │ food │
├───────────┼───────────┤
│ "bob" │ dinner │
│ "bob" │ breakfast │
└───────────┴───────────┘
重写上述查询以使用公用表表达式
WITH users_data AS (
SELECT jsonb_array_elements(summary->'users') as users FROM orders
), user_food AS (
SELECT users->'user' as user_name, jsonb_array_elements(users->'food')->>'name' as food
FROM users_data
WHERE (users->'food') is not null
) SELECT * FROM user_food;
现在我们只需按user_name
WITH users_data AS (
SELECT jsonb_array_elements(summary->'users') as users FROM orders
), user_food AS (
SELECT users->'user' as user_name, jsonb_array_elements(users->'food')->>'name' as food
FROM users_data
WHERE (users->'food') is not null
) SELECT user_name, array_agg(food) foods FROM user_food GROUP BY user_name;
最终结果
┌───────────┬────────────────────┐
│ user_name │ foods │
├───────────┼────────────────────┤
│ "bob" │ {dinner,breakfast} │
└───────────┴────────────────────┘
这是我能想到的最好的。如果你找到更好的方法,请告诉我。