MySQL 在SQL中解析JSON字段

jopen 9年前

仅限 MySQL 5.7.9以后版本支持。

mysql> SELECT c, JSON_EXTRACT(c, "$.id"), g > FROM jemp > WHERE JSON_EXTRACT(c, "$.id") > 1 > ORDER BY JSON_EXTRACT(c, "$.name"); +-------------------------------+-----------+------+  | c                             | c->"$.id" | g    |  +-------------------------------+-----------+------+  | {"id": "3", "name": "Barney"} | "3"       |    3 |  | {"id": "4", "name": "Betty"}  | "4"       |    4 |  | {"id": "2", "name": "Wilma"}  | "2"       |    2 |  +-------------------------------+-----------+------+  3 rows in set (0.00 sec)    mysql> SELECT c, c->"$.id", g > FROM jemp > WHERE c->"$.id" > 1 > ORDER BY c->"$.name"; +-------------------------------+-----------+------+  | c                             | c->"$.id" | g    |  +-------------------------------+-----------+------+  | {"id": "3", "name": "Barney"} | "3"       |    3 |  | {"id": "4", "name": "Betty"}  | "4"       |    4 |  | {"id": "2", "name": "Wilma"}  | "2"       |    2 |  +-------------------------------+-----------+------+  3 rows in set (0.00 sec)


官方文档

http://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html#operator_json-column-path

来自: http://my.oschina.net/Rayn/blog/599357