如何 select 来自 JSON 的值到 MySQL?

How to select values from JSON in MySQL?

谁能告诉我这个查询有什么问题?

DECLARE @json LONGTEXT;

SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';

##SELECT @json;

SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                name VARCHAR(40)  PATH '$.name',
                address VARCHAR(100) PATH '$.address'));

我得到的错误是:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(@json, '$[*]' COLUMNS ( name VARCHAR(40) PATH '$.name', ' at line 1

仅供参考 我正在使用 TOAD 作为连接到我的云我的 sql 实例的工具。

首先,在 mysql 中,您不需要声明变量,在这种情况下。只需使用 'SET' 关键字。最后,您需要为 'select' 查询设置一个别名。像这样:

SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';

##SELECT @json;

SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                `name` VARCHAR(40)  PATH '$.name',
                `address` VARCHAR(100) PATH '$.address')) AS T;

Ideally you should be upgrading to MYSQL 8, this answer is more meant to have fun

在 MySQL 8

以下的版本中,您需要为 simulate/emulate MySQL 的 8.0 JSON_TABLE() 编写棘手的 SQL

查询

SET @json = '[ { "name":"John Smith",  "address":"780 Mission St, San Francisco, CA 94103"}, { "name":"Sally Brown",  "address":"75 37th Ave S, St Cloud, MN 94103"}, { "name":"John Johnson",  "address":"1262 Roosevelt Trail, Raymond, ME 04071"}     ]';

##SELECT @json;

SELECT * FROM JSON_TABLE (@json, '$[*]' COLUMNS (
                `name` VARCHAR(40)  PATH '$.name',
                `address` VARCHAR(100) PATH '$.address')) AS T;

结果

| name         | address                                 |
| ------------ | --------------------------------------- |
| John Smith   | 780 Mission St, San Francisco, CA 94103 |
| Sally Brown  | 75 37th Ave S, St Cloud, MN 94103       |
| John Johnson | 1262 Roosevelt Trail, Raymond, ME 04071 |

demo

MySQL 的 5.7 simulate/emulate 查询涉及使用数字生成器和多个本机 MySQL 的 JSON 函数。

查询

SELECT 
   REPLACE(JSON_EXTRACT(json_record.json, CONCAT('$[',number,'].name')), '"', '') AS name
 , REPLACE(JSON_EXTRACT(json_record.json, CONCAT('$[',number,'].address')), '"', '') AS address
FROM (
   SELECT 
     @row := @row + 1 AS number
   FROM (
      SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION   SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
      ) row1
      CROSS JOIN (
      SELECT 0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION  SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9
    ) row2
    CROSS JOIN (
      SELECT @row := -1 
    ) init_user_params 
  ) AS number_generator
CROSS JOIN (
SELECT 
   JSON_LENGTH(json_information.json) - 1 AS json_length
 , json_information.json
FROM (
  SELECT 
    record.json
  FROM (
    SELECT 
      '
      [{
            "name": "John Smith",  
            "address": "780 Mission St, San Francisco, CA 94103"
      }, {
           "name": "Sally Brown",
           "address": "75 37th Ave S, St Cloud, MN 94103"
      }, {
           "name": "John Johnson",
           "address": "1262 Roosevelt Trail, Raymond, ME 04071"
      }]
     ' AS json
    FROM 
     DUAL   
  ) AS record  
) AS json_information

) AS json_record
WHERE 
 number BETWEEN 0 AND json_length           

结果

| name         | address                                 |
| ------------ | --------------------------------------- |
| John Smith   | 780 Mission St, San Francisco, CA 94103 |
| Sally Brown  | 75 37th Ave S, St Cloud, MN 94103       |
| John Johnson | 1262 Roosevelt Trail, Raymond, ME 04071 |

demo