0

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!

1
  • Why is ARG in your desired result set? isn't it present twice in your table? Commented Oct 17, 2014 at 14:41

2 Answers 2

2

I'm not sure why you want to use group concat. The desired result can be accomplished easily with union, which removes duplicates automatically.

SELECT country1 AS countries FROM tab
UNION
SELECT country2 FROM tab
UNION
SELECT country3 FROM tab
ORDER BY 1;
Sign up to request clarification or add additional context in comments.

Comments

1

You get duplicates because you are eliminating duplicates only within each column, not overall. You probably want something more like this:

SELECT GROUP_CONCAT(country)
  FROM (
    SELECT DISTINCT country1 AS country FROM tbl
    UNION
    SELECT DISTINCT country2 AS country FROM tbl
    UNION
    SELECT DISTINCT country3 AS country FROM tbl
  )

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.