为 SQL 查询中的 2 列选择 Distinct
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3679931/
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
Select Distinct for 2 columns in SQL query
提问by Matt
If I have a table such as
如果我有一张桌子,比如
1 bob
1 ray
1 bob
1 ray
2 joe
2 joe
And I want to select distinct based on the two columns so that I would get
我想根据两列选择不同的,这样我就可以得到
1 bob
1 ray
2 joe
How can I word my query? Is the only way to concatenate the columns and wrap them around a distinct function operator?
我该如何表达我的查询?是连接列并将它们包装在不同的函数运算符周围的唯一方法吗?
回答by Denis Valeev
select distinct id, name from [table]
or
或者
select id, name from [table] group by id, name
回答by Pablo Santa Cruz
You can just do:
你可以这样做:
select distinct col1, col2 from your_table;
That's exactly what the distinctoperator is for: removing duplicate result rows.
这正是distinct运算符的用途:删除重复的结果行。
Keep in mind that distinctis usually a pretty expensive operation, since, after processing the query, the DB server might perform a sortoperation in order to remove the duplicates.
请记住,distinct通常是一项非常昂贵的操作,因为在处理查询之后,数据库服务器可能会执行排序操作以删除重复项。