在表中,可能會包含重復值,這並不成問題。不過,有時您也許希望僅僅列出不同(distinct)的值。關鍵詞distinct用於返回唯一不同的值。
1.作用於單列
select distinct name from A
2.作用於多列
示例2.1
select distinct name, id from A
實際上是根據name和id兩個字段來去重的,而且是分別作用於了name和id列。
3.COUNT統計
select count(distinct name) from A; --表中name去重后的數目
count是不能統計多個字段的。若想使用,請使用嵌套查詢,如下:
select count(*) from (select distinct xing, name from B) AS M;
4.distinct必須放在開頭
select id, distinct name from A; --會提示錯誤,因為distinct必須放在開頭
5.其他
distinct語句中select顯示的字段只能是distinct指定的字段,其他字段是不可能出現的。例如,假如表A有“備注”列,如果想獲取distinc name,以及對應的“備注”字段,想直接通過distinct是不可能實現的。
轉自:http://www.cnblogs.com/rainman/archive/2013/05/03/3058451.html