如何将 MySQL JSON 数组转换为逗号分隔的字符串
How to convert MySQL JSON array to comma separated string
我在一列中有以下 phone 个数字:
["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]
我怎样才能得到这样的信息:
+63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403
我认为这是最唯一的 MySQL 干净的方法,至少对于 MySQL 8
以下的版本
查询
SET SESSION group_concat_max_len = @@max_allowed_packet;
SELECT
GROUP_CONCAT(
JSON_UNQUOTE(
JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']'))
)
)
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
, JSON_LENGTH(records.json) AS json_array_length
FROM (
SELECT
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]' AS json
FROM
DUAL
) AS records
) AS records
WHERE
number BETWEEN 0 AND json_array_length - 1
结果
| GROUP_CONCAT(
JSON_UNQUOTE(
JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']'))
)
) |
| -------------------------------------------------------------------------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |
见demo
Have you heard of JSON_TABLE()? – oysteing
我有,我不认为每个人都已经在 MySQL 8 上,但我也添加它是为了完整性。
MySQL 8.0 仅查询
SET SESSION group_concat_max_len = @@max_allowed_packet;
SELECT
GROUP_CONCAT(item)
FROM JSON_TABLE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, "$[*]"
COLUMNS (
rowid FOR ORDINALITY
, item VARCHAR(100) PATH "$"
)
) AS json_parsed
结果
| GROUP_CONCAT(item) |
| ----------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |
见demo
REPLACE()
嵌套方法比较混乱,但应该适用于所有 MySQL 版本。
SELECT
REPLACE(
REPLACE(
REPLACE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, '['
, ''
)
, ']'
, ''
)
, '"'
, ''
)
结果
| REPLACE(
REPLACE(
REPLACE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, '['
, ''
)
, ']'
, ''
)
, '"'
, ''
) |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| +63(02)3647766, +63(02)5467329, +63(02)8555522, +63(02)3642403 |
见demo
如果这不是您要执行的查询,这是一个简单的函数,您可以传递数组并取回字符串。
$data = ["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"];
function MakeAstring($data){
$array_with_comma = array();
$last_key = end(array_keys($data));
foreach($data as $key => $number)
{
$string .= $number.($key != $last_key ? ',': '');
}
return $string;
}
echo MakeAstring($data);
所以如果你有 replace replace 替换功能。它超级酷而且简单。
如果要构建逗号分隔的 varchar 值,请使用以下内容;
@json_array = ["value1", "value2"]
select replace(replace(replace(json_extract(@json_array, '$'), '"', '\''), '[', ''), ']', '');
但是如果你想构建数字然后使用
select replace(replace(replace(json_extract(@json_array, '$'), '"', ''), '[', ''), ']', '');enter code here
最适合所有MySQL版本的解决方案是:
SELECT
REPLACE(
REPLACE(
REPLACE(
records.json,
'[',
''
),
']',
''
),
'"',
''
) AS comma_separated
FROM (
SELECT
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]' AS json
) AS records;
看起来很重,但它很有魅力。
我在一列中有以下 phone 个数字:
["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]
我怎样才能得到这样的信息:
+63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403
我认为这是最唯一的 MySQL 干净的方法,至少对于 MySQL 8
以下的版本查询
SET SESSION group_concat_max_len = @@max_allowed_packet;
SELECT
GROUP_CONCAT(
JSON_UNQUOTE(
JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']'))
)
)
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
, JSON_LENGTH(records.json) AS json_array_length
FROM (
SELECT
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]' AS json
FROM
DUAL
) AS records
) AS records
WHERE
number BETWEEN 0 AND json_array_length - 1
结果
| GROUP_CONCAT(
JSON_UNQUOTE(
JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']'))
)
) |
| -------------------------------------------------------------------------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |
见demo
Have you heard of JSON_TABLE()? – oysteing
我有,我不认为每个人都已经在 MySQL 8 上,但我也添加它是为了完整性。
MySQL 8.0 仅查询
SET SESSION group_concat_max_len = @@max_allowed_packet;
SELECT
GROUP_CONCAT(item)
FROM JSON_TABLE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, "$[*]"
COLUMNS (
rowid FOR ORDINALITY
, item VARCHAR(100) PATH "$"
)
) AS json_parsed
结果
| GROUP_CONCAT(item) |
| ----------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |
见demo
REPLACE()
嵌套方法比较混乱,但应该适用于所有 MySQL 版本。
SELECT
REPLACE(
REPLACE(
REPLACE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, '['
, ''
)
, ']'
, ''
)
, '"'
, ''
)
结果
| REPLACE(
REPLACE(
REPLACE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, '['
, ''
)
, ']'
, ''
)
, '"'
, ''
) |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| +63(02)3647766, +63(02)5467329, +63(02)8555522, +63(02)3642403 |
见demo
如果这不是您要执行的查询,这是一个简单的函数,您可以传递数组并取回字符串。
$data = ["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"];
function MakeAstring($data){
$array_with_comma = array();
$last_key = end(array_keys($data));
foreach($data as $key => $number)
{
$string .= $number.($key != $last_key ? ',': '');
}
return $string;
}
echo MakeAstring($data);
所以如果你有 replace replace 替换功能。它超级酷而且简单。 如果要构建逗号分隔的 varchar 值,请使用以下内容;
@json_array = ["value1", "value2"]
select replace(replace(replace(json_extract(@json_array, '$'), '"', '\''), '[', ''), ']', '');
但是如果你想构建数字然后使用
select replace(replace(replace(json_extract(@json_array, '$'), '"', ''), '[', ''), ']', '');enter code here
最适合所有MySQL版本的解决方案是:
SELECT
REPLACE(
REPLACE(
REPLACE(
records.json,
'[',
''
),
']',
''
),
'"',
''
) AS comma_separated
FROM (
SELECT
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]' AS json
) AS records;
看起来很重,但它很有魅力。