MySQL query with DISTINCT keyword

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

My MySQL table country_phone_codes looks something like this

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


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    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;
    

提交回复
热议问题