您好,有人可以告诉我此查询出了什么问题。
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'));
我得到的错误是
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本以使用正确的语法 靠近'(@json,'$ [*]'栏( 名称VARCHAR(40)PATH'$ .name', '在第1行
仅供参考,我使用TOAD作为工具将sql实例连接到云。
答案 0 :(得分:2)
首先,在这种情况下,您无需在mysql中声明变量。只需使用“ SET”关键字即可。最后,您需要为“选择”查询放置一个别名。像这样:
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;
答案 1 :(得分:1)
理想情况下,您应该升级到MYSQL 8,此答案更倾向于玩得开心
您需要编写棘手的SQL才能在MySQL 8以下的版本中模拟/仿真MySQL 8.0 JSON_TABLE()
查询
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的模拟/仿真查询涉及使用数字生成器和多个本机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