MySQL 5.7是否等同于PHP的json_decode()?

时间:2018-07-12 11:46:27

标签: php mysql stored-procedures

我正在将一些php脚本转换为mysql存储过程,并在结果集的数据上遇到了php的json_decode()的用法。它发生在存储为中间文本的字段中,utf8,utf8_unicode_ci

如何使用纯MySQL执行'json_decode()'过程?

从字段中采样数据:

  

{“ 93489”:{“ X1”:{“ net”:164,“ vat”:33.6},“ X2”:{“ net”:0,“ vat”:0}}}}

1 个答案:

答案 0 :(得分:1)

您可以使用JSON_EXTRACT功能:

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)