简体   繁体   English

从具有包含相似数据的多列的表中选择唯一值

[英]SELECT unique values from table with multiple columns containing similar data

I'm trying to select a list of distinct country codes from a table that has 3 different columns that contain country code data. 我正在尝试从具有3个包含国家代码数据的不同列的表中选择不同国家代码的列表。

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! 任何帮助表示赞赏!

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;

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
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM