在 MySQL 5.7 兼容的 Amazon Aurora 中调用 JSON_TABLE() 时出错
Error when calling JSON_TABLE() in MySQL 5.7–compatible Amazon Aurora
我在 MySQL 5.7 兼容的 Amazon Aurora 中尝试使用 JSON_TABLE()
函数时出现以下错误。
Error Code: 1064. 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_col, '$.people[*]' COLUMNS ( name VARCHAR(40) PATH '$.na' at line 1
在Amzon Mysql JSON Documentation中说明它支持很多JSON功能。但是JSON_TABLE
不在其中。
我可以在 Mysql 8(不是 AWS Aurora)中执行下面的查询,它给了我下面的结果。
SET @json_col = '{
"people": [
{
"name": "John Smith"
},
{
"name": "Sally Brown"
},
{
"name": "John Johnson"
}
]
}';
SELECT * from JSON_TABLE(@json_col, '$.people[*]' COLUMNS (
name VARCHAR(40) PATH '$.name')
) people;
我可以在 MySQL Aurora 5.7 中使用任何替代方法来实现上述结果。 我尝试替换 JSON_EXTRACT
。但是,它会导致显示与上述相同的错误。
SELECT JSON_UNQUOTE(JSON_EXTRACT(@json_col, CONCAT('$.people[', num, '].name'))) name
FROM ( SELECT 0 num UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 ) numbers
HAVING name IS NOT NULL;
如果您不介意临时 table 和存储过程的复杂性,本文将创建一个临时 table 加入反对:
drop temporary table if exists temp_numbers;
create TEMPORARY TABLE IF NOT EXISTS temp_numbers(
num INT
);
drop procedure if exists fill_num;
delimiter //
create procedure fill_num(in num int)
begin
declare i int default 0;
while (i < num) do
insert into temp_numbers values (i);
set i = i + 1;
end while;
end
//
delimiter ;
SET @json_col = '{
"people": [
{
"name": "John Smith"
},
{
"name": "Sally Brown"
},
{
"name": "John Johnson"
}
]
}';
set @json_people = json_extract(@json_col,'$.people[*]');
call fill_num(json_length(@json_people));
select json_unquote(json_extract(@json_people,concat('$[',num,'].name'))) from temp_numbers;
我在 MySQL 5.7 兼容的 Amazon Aurora 中尝试使用 JSON_TABLE()
函数时出现以下错误。
Error Code: 1064. 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_col, '$.people[*]' COLUMNS ( name VARCHAR(40) PATH '$.na' at line 1
在Amzon Mysql JSON Documentation中说明它支持很多JSON功能。但是JSON_TABLE
不在其中。
我可以在 Mysql 8(不是 AWS Aurora)中执行下面的查询,它给了我下面的结果。
SET @json_col = '{
"people": [
{
"name": "John Smith"
},
{
"name": "Sally Brown"
},
{
"name": "John Johnson"
}
]
}';
SELECT * from JSON_TABLE(@json_col, '$.people[*]' COLUMNS (
name VARCHAR(40) PATH '$.name')
) people;
我可以在 MySQL Aurora 5.7 中使用任何替代方法来实现上述结果。 我尝试替换 JSON_EXTRACT
。但是,它会导致显示与上述相同的错误。
SELECT JSON_UNQUOTE(JSON_EXTRACT(@json_col, CONCAT('$.people[', num, '].name'))) name
FROM ( SELECT 0 num UNION ALL
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4 UNION ALL
SELECT 5 ) numbers
HAVING name IS NOT NULL;
如果您不介意临时 table 和存储过程的复杂性,本文将创建一个临时 table 加入反对:
drop temporary table if exists temp_numbers;
create TEMPORARY TABLE IF NOT EXISTS temp_numbers(
num INT
);
drop procedure if exists fill_num;
delimiter //
create procedure fill_num(in num int)
begin
declare i int default 0;
while (i < num) do
insert into temp_numbers values (i);
set i = i + 1;
end while;
end
//
delimiter ;
SET @json_col = '{
"people": [
{
"name": "John Smith"
},
{
"name": "Sally Brown"
},
{
"name": "John Johnson"
}
]
}';
set @json_people = json_extract(@json_col,'$.people[*]');
call fill_num(json_length(@json_people));
select json_unquote(json_extract(@json_people,concat('$[',num,'].name'))) from temp_numbers;