【问题标题】:Mysql explode json array to rowsMysql 将 json 数组分解为行
【发布时间】:2022-11-30 00:01:30
【问题描述】:

从一个包含 json 字典数组的列的表中,我需要每行提取一个键“user_id”的所有值。如果为 null 或空数组,则返回 NULL。类似于 python pandas explode 方法。

数组长度未知。

原表:

【问题讨论】:

    标签: mysql arrays json dictionary explode


    【解决方案1】:
    select id, j.user_id from mytable left outer join 
    json_table(users, '$[*]' columns (user_id int path '$.user_id')) as j on true;
    
    +------+---------+
    | id   | user_id |
    +------+---------+
    |    1 |      33 |
    |    1 |      34 |
    |    2 |    2975 |
    |    3 |    NULL |
    +------+---------+
    

    阅读https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html 了解有关 JSON_TABLE() 函数的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2016-12-11
      • 2017-02-15
      • 2018-11-13
      • 2014-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多