MySQL Select DISTINCT multiple columns based on the uniqueness of one row?

后端 未结 2 1660
你的背包
你的背包 2021-01-17 22:46

I\'m trying to understand what this query does exactly:

SELECT DISTINCT `state`, `state_name` FROM `geo` ORDER BY `state_name` ASC

All I\'m

相关标签:
2条回答
  • 2021-01-17 23:19

    DISTINCT will return only distinct rows, so:

    Is my query checking both columns for uniqueness or just state?

    Both columns

    You could also switch to GROUP BY instead.

    SELECT `state`, `state_name` FROM `geo` group by 'state', 'state_name' ORDER BY `state_name` ASC 
    
    0 讨论(0)
  • 2021-01-17 23:29

    It's checking for unique combinations of state and state_name. Distinct operates on all columns included in your select list.

    To only include unique state values, just select distinct state

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