6

我在一列中有以下电话号码:

["+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

4 回答 4

7

我认为这是最唯一的 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                                                                |

演示

你听说过 JSON_TABLE() 吗?– 牡蛎

我有,我不认为每个人都已经在 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 |

演示

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                                                                                                                                      |

演示

于 2019-07-01T12:16:12.590 回答
4

所以如果你有替换替换替换功能。它超级酷而且简单。如果要构建以逗号分隔的 varchar 值,请使用以下命令;

@json_array = ["value1", "value2"]

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

但是如果你想建立数字然后使用

select replace(replace(replace(json_extract(@json_array, '$'), '"', ''), '[', ''), ']', '');enter code here
于 2020-09-11T17:40:21.387 回答
0

最适合所有 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;

看起来很重,但它就像一个魅力。

于 2021-08-17T11:49:20.577 回答
-1

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

$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);
于 2019-07-01T12:26:41.800 回答