SQL中distinct用法


在表中,可能會包含重復值,這並不成問題。不過,有時您也許希望僅僅列出不同(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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM