Mysql
 sql >> Database >  >> RDS >> Mysql

Come convertire l'array JSON MySQL in una stringa separata da virgole

penso che questo sia l'unico modo MySQL pulito, almeno per le versioni di MySQL inferiori a 8

Richiesta

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 

Risultato

| GROUP_CONCAT(
     JSON_UNQUOTE(
       JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']'))
     )
  ) |
| -------------------------------------------------------------------------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403                                                                |

vedi demo

Ho, non presumo che tutti siano già su MySQL 8, ma l'ho aggiunto anche per completezza.

Solo query 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  

Risultato

| GROUP_CONCAT(item)                                          |
| ----------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |

vedi demo

Il REPLACE() il metodo di annidamento è più disordinato, ma dovrebbe funzionare su tutte le versioni di MySQL.

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

Risultato

| 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                                                                                                                                      |

vedi demo