如何将MySQL JSON数组转换为逗号分隔的字符串

时间:2019-07-01 12:05:45

标签: mysql json mysql-json

我在栏中输入以下电话号码:

["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]

我如何获得这样的信息:

+63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403

4 个答案:

答案 0 :(得分:4)

我认为这是最唯一的MySQL清除方式,至少适用于8以下的MySQL版本

查询

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

  

您听说过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

答案 1 :(得分:0)

如果不是您要在此处执行的查询,则它是一个简单的函数,您可以传递数组并获取字符串。

$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);

答案 2 :(得分:0)

我不确定在您的情况下,仅链接替换不会更简单,即:

replace(replace(replace(phonenumbers, '"', ''), '[', ''), ']', '')

如果确实需要将其删除,则再提供一个空间。

答案 3 :(得分:0)

因此,如果您具有replace,则请替换replace功能。它超级酷和容易。 如果要构建逗号分隔的varchar值,请使用以下内容;

@json_array = [“ value1”,“ value2”]

let a = [];
let n = prompt("enter a number: ");

if (n >= 1) {
  a.push(0);
}
if (n >= 2) {
  a.push(1);
}

for (let i = 2; i < n; i++) {
    a.push(a[i - 1] + a[i - 2]);
}

console.log(a);

但是,如果要构建数字,则使用

select replace(replace(replace(json_extract(@json_array, '$'), '"', '\''), '[', ''), ']', '');