简体   繁体   English

Select SQL 查询中的 2 列不同

[英]Select Distinct for 2 columns in SQL query

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我想 select 基于两列不同,这样我就可以得到

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?是连接列并将它们包裹在不同的 function 运算符周围的唯一方法吗?

select distinct id, name from [table]

要么

select id, name from [table] group by id, name

You can just do: 您可以这样做:

select distinct col1, col2 from your_table;

That's exactly what the distinct operator is for: removing duplicate result rows. 这正是不同运算符的用途:删除重复的结果行。

Keep in mind that distinct is usually a pretty expensive operation, since, after processing the query, the DB server might perform a sort operation in order to remove the duplicates. 请记住, distinct通常是非常昂贵的操作,因为在处理查询之后,DB服务器可能会执行排序操作以删除重复项。

@Pablo Santa Cruz: it does not remove duplicates from any columns. @Pablo Santa Cruz:它不会从任何列中删除重复项。

SELECT DISTINCT prof1, prof2 
FROM programmer

Results in:结果是:

ASSEMBLY    C
CLIPPER COBOL
COBOL   DBASE
PASCAL  BASIC
PASCAL  CLIPPER
PASCAL  DBASE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM