SQL select distinct的使用方法,selectdistinct

來源:互聯網
上載者:User

SQL select distinct的使用方法,selectdistinct

在表中,可能會包含重複值。這並不成問題,不過,有時您也許希望僅僅列出不同(distinct)的值。關鍵詞 distinct用於返回唯一不同的值。

表A:

樣本1

複製代碼 代碼如下:
select distinct name from A

執行後結果如下:

樣本2
select distinct name, id from A

執行後結果如下:

實際上是根據“name+id”來去重,distinct同時作用在了name和id上,這種方式Access和SQL Server同時支援。

樣本3:統計

select count(distinct name) from A; --表中name去重後的數目, SQL Server支援,而Access不支援
select count(distinct name, id) from A; --SQL Server和Access都不支援

樣本4

select id, distinct name from A; --會提示錯誤,因為distinct必須放在開頭

其他

distinct語句中select顯示的欄位只能是distinct指定的欄位,其他欄位是不可能出現的。例如,假如表A有“備忘”列,如果想擷取distinc name,以及對應的“備忘”欄位,想直接通過distinct是不可能實現的。
但可以通過其他方法實現關於SQL Server將一列的多行內容拼接成一行的問題討論

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.