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.
精彩评论