I have the following phone numbers in a column:
["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]
How can I get that info like this:
+63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403
An elegant solution is to use JSON_TABLE() and MySQL GROUP_CONCAT() capabilities. Save this answer. Show activity on this post. $array = (result from column); echo implode(",", $array); // separates array variables with a comma ",".
MySQL supports a native JSON data type defined by RFC 7159 that enables efficient access to data in JSON (JavaScript Object Notation) documents.
Note that any database will accept JSON documents as a single string blob. However, MySQL and PostgreSQL support validated JSON data in real key/value pairs rather than a basic string.
MySQL provides two operators ( -> and ->> ) to extract data from JSON columns. ->> will get the string value while -> will fetch value without quotes. As you can see ->> returns output as quoted strings, while -> returns values as they are. You can also use these operators in WHERE clause as shown below.
i think this is the most only MySQL clean way, atleast for MySQL versions under 8
Query
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
Result
| GROUP_CONCAT(
JSON_UNQUOTE(
JSON_EXTRACT(records.json, CONCAT('$[', number_generator.number , ']'))
)
) |
| -------------------------------------------------------------------------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |
see demo
Have you heard of JSON_TABLE()? – oysteing
I have, i dont assume everybody to be on MySQL 8 already but i added it for completeness also.
MySQL 8.0 query only
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
Result
| GROUP_CONCAT(item) |
| ----------------------------------------------------------- |
| +63(02)3647766,+63(02)5467329,+63(02)8555522,+63(02)3642403 |
see demo
The REPLACE()
nesting method is more messy, but should work on all MySQL versions.
SELECT
REPLACE(
REPLACE(
REPLACE(
'["+63(02)3647766", "+63(02)5467329", "+63(02)8555522", "+63(02)3642403"]'
, '['
, ''
)
, ']'
, ''
)
, '"'
, ''
)
Result
| 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 |
see demo
so if you have replace replace replace function. Its super cool and easy. if you want to build varchar values comma separated then use the following;
@json_array = ["value1", "value2"]
select replace(replace(replace(json_extract(@json_array, '$'), '"', '\''), '[', ''), ']', '');
But if you want to build numeric then use
select replace(replace(replace(json_extract(@json_array, '$'), '"', ''), '[', ''), ']', '');enter code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With