[英]query JSON array column in mysql into rows
我有一張表格,上面有類似的數據
| user_id | favorite_foods |
|--------- |-------------------------------------- |
| user1 | ["milk","cake"] |
| user2 | null |
| user3 | ["cake","hotdogs"] |
| user4 | ["cheese","apples","cake","hotdogs"] |
而且我想將數組中的數據提取為更規范化的形式,例如
| user1 | milk |
| user1 | cake |
| user2 | null |
| user3 | cake |
| user3 | hotdogs |
| user4 | cheese |
| user4 | apples |
| user4 | cake |
| user4 | hotdogs |
似乎如果這是可能的,它將是JSON_EXTRACT
但我沒有看到任何關於是否可以為每個路徑表達式結果輸出一行的文檔,以便在路徑結果旁邊輸出其他非 JSON 列。
select user_id, j.food from ihaveatablewithdatalike
cross join json_table(favorite_foods, '$[*]' columns (
food varchar(20) path '$')) as j;
+---------+---------+
| user_id | food |
+---------+---------+
| user1 | milk |
| user1 | cake |
| user3 | cake |
| user3 | hotdogs |
| user4 | cheese |
| user4 | apples |
| user4 | cake |
| user4 | hotdogs |
+---------+---------+
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.