MySQL query with DISTINCT keyword

后端 未结 5 812
醉酒成梦
醉酒成梦 2021-01-24 05:23

My MySQL table country_phone_codes looks something like this

id     country_code     area_code     name
---------------------------------------------------------         


        
相关标签:
5条回答
  • 2021-01-24 05:34

    As you seem to want to select those rows, where the area_code is the same as country_code, you could just select those rows where area_code and country_code are equal:

    SELECT country_code, name
    FROM country_phone_codes
    WHERE area_code = country_code;
    

    If there is possibility that there are multiple rows with the same area code and country code, you can use DISTINCT to select only one row for each (country_code, name) tuple.

    SELECT DISTINCT country_code, name
    FROM country_phone_codes
    WHERE area_code = country_code;
    
    0 讨论(0)
  • 2021-01-24 05:40

    The answer is trivial, using GROUP BY:

    SELECT country_code,MIN(name)
    FROM country_phone_codes
    GROUP BY country_code;
    

    The DISTINCT function and ORDER BY aren’t necessary with GROUP BY. As the original question specifically pertained to MySQL, the MIN() aggregate function isn’t necessary and you might see better performance without it if all of the following are true:

    • The server is MySQL
    • The storage engine is InnoDB
    • The first column of the example data is the primary key and the entries follow the same ordering suggested by the small sample, namely, the country name appears before all other names in the group.

    This works because the InnoDB storage engine will scan in the order of the primary key and, for nonaggregated columns, it will use the first value it finds.

    0 讨论(0)
  • 2021-01-24 05:46

    To select distinct country_code, name pairs:

    select country_code, name
    from country_phone_codes
    where country_code = area_code;
    
    0 讨论(0)
  • 2021-01-24 05:52

    Seems like you have a one-to-many relationship between country_code and name.

    If you do a simple GROUP BY query like

    SELECT country_code,name FROM country_phone_codes GROUP BY country_code
    

    you might end up with

    country_code    name
    -----------------------------
    93            |  AFGHANISTAN
    355           |  ALBANIA
    213           |  ALGERIA
    124           |  COUNTRY - MOBILE
    -----------------------------
    

    assuming all your country names are

    COUNTRY
    COUNTRY - SOMETHING
    COUNTRY - SOMETHING - SOMETHING
    

    would be better to use

    SELECT country_code,MIN(name) FROM country_phone_codes GROUP BY country_code
    

    so you end up with

    country_code    name
    -----------------------------
    93            |  AFGHANISTAN
    355           |  ALBANIA
    213           |  ALGERIA
    xxx           |  COUNTRY
    -----------------------------
    

    this is assuming that you have both of those records in your table

    id     country_code     area_code     name
    ------------------------------------------------------------
    xx   |   xxx          |  xx        |  COUNTRY
    xx   |   xxx          |  xx        |  COUNTRY - SOMETHING
    ------------------------------------------------------------
    
    0 讨论(0)
  • 2021-01-24 05:57

    You need to group your country_code also then & then only you will get correct matching results.

    SELECT country_code,name
    FROM country_phone_codes 
    group by country_code
    ORDER BY country_code
    

    And we should need to avoid use of Distinct in select clause as it gives performance issues. Instead of that we can use group by

    0 讨论(0)
提交回复
热议问题