Home » Mysql » MySQL select * with distinct id

MySQL select * with distinct id

Posted by: admin November 29, 2017 Leave a comment

Questions:

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!

Answers:

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