Reputation: 4076
I am trying to select a row with a distinct id, yet return all the fields.
SELECT * DISTINCT(ID) FROM table WHERE ...
I ultimately need the ID, City, State and Zip. How can I get rows that are not duplicate ID's and return all fields into my mysql_fetch_array?
I have tried the following:
SELECT * DISTINCT(ID) FROM table WHERE ...
SELECT DISTINCT ID * FROM table WHERE ...
SELECT ID,City,State,Zip DISTINCT ID FROM ...
SELECT ID,City,State,Zip DISTINCT(ID) FROM ...
I was reading other questions here and none seem to help. Thanks in advance!
Upvotes: 35
Views: 89521
Reputation: 56769
Try using GROUP BY
:
select id, city, state, zip
from mytable
group by id
Note that this will return an arbitrary address for each id
if there are duplicates.
Demo: http://www.sqlfiddle.com/#!2/c0eba/1
Upvotes: 52