在表中,可能會包含重復(fù)值。這并不成問題,不過,有時您也許希望僅僅列出不同(distinct)的值。關(guān)鍵詞 distinct用于返回唯一不同的值。
表A:
示例1
執(zhí)行后結(jié)果如下:
示例2
select distinct name, id from A
執(zhí)行后結(jié)果如下:
實(shí)際上是根據(jù)“name+id”來去重,distinct同時作用在了name和id上,這種方式Access和SQL Server同時支持。
示例3:統(tǒng)計(jì)
select count(distinct name) from A; --表中name去重后的數(shù)目, SQL Server支持,而Access不支持
select count(distinct name, id) from A; --SQL Server和Access都不支持
示例4
select id, distinct name from A; --會提示錯誤,因?yàn)閐istinct必須放在開頭
其他
distinct語句中select顯示的字段只能是distinct指定的字段,其他字段是不可能出現(xiàn)的。例如,假如表A有“備注”列,如果想獲取distinc name,以及對應(yīng)的“備注”字段,想直接通過distinct是不可能實(shí)現(xiàn)的。
但可以通過其他方法實(shí)現(xiàn)關(guān)于SQL Server將一列的多行內(nèi)容拼接成一行的問題討論
新聞熱點(diǎn)
疑難解答
圖片精選