select
(SELECT group_concat(DISTINCT a) FROM my_table) as a,
(SELECT group_concat(DISTINCT b) FROM my_table) as b,
(SELECT group_concat(DISTINCT c) FROM my_table) as c,
(SELECT group_concat(DISTINCT d) FROM my_table) as d
This will give DISTINCT values across all the columns:
SELECT DISTINCT value
FROM (
SELECT DISTINCT a AS value FROM my_table
UNION SELECT DISTINCT b AS value FROM my_table
UNION SELECT DISTINCT c AS value FROM my_table
) AS derived
You can simply use this to get back one row with four comma separated lists with all values.
SELECT
GROUP_CONCAT(DISTINCT `a`) AS `as`,
GROUP_CONCAT(DISTINCT `b`) AS `bs`,
GROUP_CONCAT(DISTINCT `c`) AS `cs`,
GROUP_CONCAT(DISTINCT `d`) AS `ds`
FROM
`my_table`;
If you need an other separator than the comma, add the SEPARATOR option to GROUP_CONCAT.
Based on your answers I made the same but for extracting a json column vars with arrays of 8 values all merged in 1 column with quotes trimed distinct values.
SELECT DISTINCT TRIM(BOTH '"' FROM T1.`vars`) FROM (
SELECT JSON_EXTRACT(`vars`, '$[1]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[2]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[3]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[4]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[5]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[6]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[7]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
UNION
SELECT JSON_EXTRACT(`vars`, '$[8]') AS `vars`
FROM my_table
WHERE `vars` != '[]'
) T1
WHERE `vars` IS NOT NULL
AND `vars` != 'false';