php MySQL选择*具有不同的ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12075502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:45:43  来源:igfitidea点击:

MySQL select * with distinct id

phpmysql

提问by NotJay

I am trying to select a row with a distinct id, yet return all the fields.

我正在尝试选择具有不同 id 的行,但返回所有字段。

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?

我最终需要 ID、城市、州和邮编。如何获取不重复 ID 的行并将所有字段返回到我的 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!

我在这里阅读其他问题,但似乎没有任何帮助。提前致谢!

回答by mellamokb

Try using GROUP BY:

尝试使用GROUP BY

  select id, city, state, zip
    from mytable
group by id

Note that this will return an arbitrary address for each idif there are duplicates.

请注意,id如果存在重复项,这将为每个地址返回一个任意地址。

Demo: http://www.sqlfiddle.com/#!2/c0eba/1

演示:http://www.sqlfiddle.com/#!2/ c0eba/1