MySQL Return JSON Value Attributes

mysql> SELECT *
    -> FROM
    ->   JSON_TABLE(
    ->     '[{"a":"3"},{"a":2},{"b":1},{"a":0},{"a":[1,2]}]', --json
    ->     "$[*]"  -- json //取json所有数据
    ->     COLUMNS(
    ->       rowid FOR ORDINALITY,  --//列名 递增
    ->       ac VARCHAR(100) PATH "$.a" DEFAULT '999' ON ERROR DEFAULT '111' ON EMPTY, --// 列名,类型,PATH,对应节点,默认值
    ->       aj JSON PATH "$.a" DEFAULT '{"x": 333}' ON EMPTY,
    ->       bx INT EXISTS PATH "$.b"
    ->     )
    ->   ) AS tt;

+-------+------+------------+------+
| rowid | ac   | aj         | bx   |
+-------+------+------------+------+
|     1 | 3    | "3"        |    0 |
|     2 | 2    | 2          |    0 |
|     3 | 111  | {"x": 333} |    1 |
|     4 | 0    | 0          |    0 |
|     5 | 999  | [1, 2]     |    0 |
+-------+------+------------+------+

参考:
https://dev.mysql.com/doc/refman/8.0/en/json-table-functions.html

你可能感兴趣的:(mysql)