user9465677
user9465677

Reputation: 427

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 e.g.

belleuve, wa, 98004 bellevue, wa, 98006

Similarly a city name can be also present in some other state e.g.

bellevue, TN, 05156

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

Upvotes: 0

Views: 13

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522626

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;

Upvotes: 1

Related Questions