I'm trying to select a list of distinct country codes from a table that has 3 different columns that contain country code data.
Example
Raw Desired
+-----------+-----------+-----------+ +-----------+
| country_1 | country_2 | country_3 | | countries |
+-----------+-----------+-----------+ +-----------+
| GBR | GBR | IRL | | ARG |
| DEU | FRA | CHE | | AUS |
| CHN | CHN | IND | | BRA |
| USA | CAN | MEX | | CAN |
| AUS | JAP | KOR | | CHE |
| BRA | ARG | ARG | | CHN |
| NOR | SWE | FIN | | DEU |
| GBR | FRA | GBR | | FIN |
| IRL | IRL | IRL | | FRA |
+-----------+-----------+-----------+ | GBR |
| IND |
| IRL |
| JAP |
| KOR |
| MEX |
| NOR |
| SWE |
| USA |
+-----------+
The closest I've come so far is with a query like:
mysql> SELECT
CONCAT(
(SELECT GROUP_CONCAT(DISTINCT country_1 ORDER BY country_1) FROM tbl),
',',
(SELECT GROUP_CONCAT(DISTINCT country_2 ORDER BY country_2) FROM tbl),
',',
(SELECT GROUP_CONCAT(DISTINCT country_3 ORDER BY country_3) FROM tbl)
) AS countries;
+-------------------------------------------------------------------------------------------------+
| countries |
+-------------------------------------------------------------------------------------------------+
| AUS,BRA,CHN,DEU,GBR,IRL,NOR,USA,ARG,CAN,CHN,FRA,GBR,IRL,JAP,SWE,ARG,CHE,FIN,GBR,IND,IRL,KOR,MEX |
+-------------------------------------------------------------------------------------------------+
But as you can see, there are still duplicates and I'm not sure how I could get them into rows either.
Any help is appreciated!
ARGin your desired result set? isn't it present twice in your table?