在MySQL中字符串化JSON数组

时间:2019-04-27 12:30:27

标签: mysql sql json

我在表中有一个JSON列。即 表格

    column
---------------
[2,5,7,21,1,54,12]

现在将其返回数组用于以下查询。

select column from table

输出    => [2,5,7,21,1,54,12]

我想要的是输出为“ 2,5,7,21,1,54,12 ”。

有什么建议吗?

3 个答案:

答案 0 :(得分:0)

以下是查询JSON数组的示例:

select data from t;
+--------------------------+
| data                     |
+--------------------------+
| [2, 5, 7, 21, 1, 54, 12] |
+--------------------------+

您可以使用JSON_UNQUOTE()将JSON数组转换为字符串。但是它用方括号和空格格式化字符串:

select json_unquote(data) as stringified from t;
+--------------------------+
| stringified              |
+--------------------------+
| [2, 5, 7, 21, 1, 54, 12] |
+--------------------------+

您可以使用REPLACE()删除那些不需要的字符:

select replace(replace(replace(json_unquote(data), ' ', ''), '[', ''), ']', '') as stringified from t;
+------------------+
| stringified      |
+------------------+
| 2,5,7,21,1,54,12 |
+------------------+

在MySQL 8.0中,您可以一次调用REGEXP_REPLACE()来替换字符:

select regexp_replace(json_unquote(data), '[\\[\\] ]', '') as stringified from t;
+------------------+
| stringified      |
+------------------+
| 2,5,7,21,1,54,12 |
+------------------+

答案 1 :(得分:0)

一个优雅的解决方案是使用 JSON_TABLE() 和 MySQL GROUP_CONCAT() 功能。

有了这样的数据样本:

+--------+--------------------------------------------------------------------+
| user   | emails (JSON)                                                      |
+--------+--------------------------------------------------------------------+
| user-1 | ["user-1@email-1.net", "user-1@email-2.net", "user-1@email-3.net"] |
| user-2 | ["user-2@email-1.net"]                                             |
| user-3 | ["user-3@email-1.net", "user-3@email-2.net"]                       |
+--------+--------------------------------------------------------------------+

如果我们想输出:

+--------+----------------------------------------------------------------+
| user   | emails (TEXT)                                                  |
+--------+----------------------------------------------------------------+
| user-1 | user-1@email-1.net // user-1@email-2.net // user-1@email-3.net |
| user-2 | user-2@email-1.net                                             |
| user-3 | user-3@email-1.net // user-3@email-2.net"                      |
+--------+----------------------------------------------------------------+

我们可以:

WITH data_sample (user, emails) AS (
  -- Fake data build to test query
  VALUES 
    ROW ('user-1', JSON_ARRAY('user-1@email-1.net', 'user-1@email-2.net', 'user-1@email-3.net')),
    ROW ('user-2', JSON_ARRAY('user-2@email-1.net')),
    ROW ('user-3', JSON_ARRAY('user-3@email-1.net', 'user-3@email-2.net'))
)
SELECT ALL user, GROUP_CONCAT(email SEPARATOR ' // ') AS emails
FROM data_sample
CROSS JOIN JSON_TABLE(emails, '$[*]' COLUMNS (email TINYTEXT PATH '$')) AS _
GROUP BY user;

答案 2 :(得分:-1)

$array = (result from column);

echo implode(",", $array); // separates array variables with a comma ",".

更多信息:
https://www.php.net/manual/en/function.implode.php