我目前有一个表,其中包含一个带有代表Twitter cashtags的JSON对象的列。
例如,这是我原来的查询:
SELECT
DATA->'id' as tweet_id,
DATA->'text' as tweet_text,
DATA->'entities'->'symbols' as cashtags
FROM documents
LIMIT 10
cashtags列将返回类似
的内容[{"text":"HEMP","indices":[0,5]},{"text":"MSEZ","indices":[63,68]}]
如何遍历此数据类型(列为jsonb),以便仅返回文本等于HEMP或MSEZ的结果?
答案 0 :(得分:1)
值data->'entities'->'symbols'
是一个json数组。您可以使用函数jsonb_array_elements(),
来取消数组,例如:
SELECT
data->'id' as tweet_id,
data->'text' as tweet_text,
value as cashtag
FROM documents,
jsonb_array_elements(data->'entities'->'symbols')
where value->>'text' in ('HEMP', 'MSEZ');
tweet_id | tweet_text | cashtag
----------+------------+---------------------------------------
1 | "my_tweet" | {"text": "HEMP", "indices": [0, 5]}
1 | "my_tweet" | {"text": "MSEZ", "indices": [63, 68]}
(2 rows)
或:
SELECT DISTINCT
data->'id' as tweet_id,
data->'text' as tweet_text,
data->'entities'->'symbols' as cashtags
FROM documents,
jsonb_array_elements(data->'entities'->'symbols')
WHERE value->>'text' in ('HEMP', 'MSEZ');
tweet_id | tweet_text | cashtags
----------+------------+------------------------------------------------------------------------------
1 | "my_tweet" | [{"text": "HEMP", "indices": [0, 5]}, {"text": "MSEZ", "indices": [63, 68]}]
(1 row)