在 MYSQL 中使用 SELECT DISTINCT

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3091349/
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-31 16:23:52  来源:igfitidea点击:

Using SELECT DISTINCT in MYSQL

sqlmysqldistinct

提问by ZaneDeFazio

Been doing a lot of searching and haven't really found an answer to my MYSQL issue.

进行了大量搜索并没有真正找到我的 MYSQL 问题的答案。

SELECT DISTINCT name, type, state, country FROM table

Results in 1,795 records

结果 1,795 条记录

SELECT DISTINCT name FROM table

Results in 1,504 records

结果为 1,504 条记录

For each duplicate "name"... "type", "state", "country" aren't matching in each record.

对于每个重复的“名称”...“类型”、“州”、“国家”在每条记录中都不匹配。

Trying to figure out how to SELECT the associated row to the DISTINCT name, without checking them for being DISTINCT or not

试图弄清楚如何将关联行选择为 DISTINCT 名称,而不检查它们是否为 DISTINCT

回答by cypher

SELECT name, type, state, country FROM table GROUP BY name;

should do the trick.

应该做的伎俩。

回答by Franti?ek ?ia?ik

If you want distinct name, you must decide which of the multiple values that may occur for each distinct name you want. For example, you may want minimals, or counts:

如果您想要不同的名称,您必须决定您想要的每个不同名称可能出现的多个值中的哪一个。例如,您可能需要最小值或计数:

SELECT name, min(type), min(state), count(country) FROM table GROUP BY name

SELECT name, min(type), min(state), count(country) FROM table GROUP BY name