简体   繁体   中英

MySQL select * with distinct id

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!

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

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