开发者

Select Distinct for 2 columns in SQL query

开发者 https://www.devze.com 2023-01-15 06:51 出处:网络
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

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 开发者_如何学JAVAword my query? Is the only way to concatenate the columns and wrap them around a distinct function operator?


select distinct id, name from [table]

or

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消