国内精品,久久人妻,国产精品久久久久影院亚瑟,国产精品视频一区国模私拍,国产精品久久这里只有精品,国产在线 | 中文

全國統(tǒng)一學(xué)習(xí)專線 8:30-21:00
位置:魔方網(wǎng) > 外語類>提升英語> 數(shù)據(jù)庫查詢distinct的用法  正文

數(shù)據(jù)庫查詢distinct的用法

發(fā)布時間:2022-06-16 17:10:23來源:魔方格


【資料圖】

  數(shù)據(jù)庫查詢distinct的用法的用法你知道嗎?

  數(shù)據(jù)庫查詢distinct的用法的用法如下:

  在表中,可能會包含重復(fù)值。這并不成問題,不過,有時您也許希望僅僅列出不同(distinct)的值。關(guān)鍵詞 distinct用于返回唯一不同的值。

  表A:

  表B:

  1.作用于單列

  select distinct name from A

  執(zhí)行后結(jié)果如下:

  2.作用于多列

  示例2.1

  select distinct name, id from A

  執(zhí)行后結(jié)果如下:

  實際上是根據(jù)name和id兩個字段來去重的,這種方式Access和SQL Server同時支持。

  示例2.2

  select distinct xing, ming from B

  返回如下結(jié)果:

  返回的結(jié)果為兩行,這說明distinct并非是對xing和ming兩列“字符串拼接”后再去重的,而是分別作用于了xing和ming列。

  3.COUNT統(tǒng)計

  select count(distinct name) from A; --表中name去重后的數(shù)目, SQL Server支持,而Access不支持

  count是不能統(tǒng)計多個字段的,下面的SQL在SQL Server和Access中都無法運行。

  select count(distinct name, id) from A;

  若想使用,請使用嵌套查詢,如下:

  select count(*) from (select distinct xing, name from B) AS M;

  4.distinct必須放在開頭

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

  5.其他

  distinct語句中select顯示的字段只能是distinct指定的字段,其他字段是不可能出現(xiàn)的。例如,假如表A有“備注”列,如果想獲取distinc name,以及對應(yīng)的“備注”字段,想直接通過distinct是不可能實現(xiàn)的。但可以通過其他方法實現(xiàn)關(guān)于SQL Server將一列的多行內(nèi)容拼接成一行的問題討論

相關(guān)內(nèi)容:

相關(guān)文章
最新文章
相關(guān)學(xué)校
相關(guān)熱詞