简体   繁体   中英

Mysql Unique / distinct rows based on two or more columns

I have a table where I have the following columns - lets call location table city, state, zipcode, lat, lng

The problem is that every city and state can multiple zip codes eg

belleuve, wa, 98004 bellevue, wa, 98006

Similarly a city name can be also present in some other state eg

bellevue, TN, 05156

How do I select a distinct city eg Bellevue for each state. Basically the result should show both Bellevue, WA and Bellevue, TN but for WA only one occurance of Bellevue.

Grouping by the city and state should work here:

SELECT City, State
FROM yourTable
GROUP BY City, State;

We also could use DISTINCT :

SELECT DISTINCT City, State
FROM yourTable;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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