萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> mysql sql DISTINCT 刪除重復記錄

mysql sql DISTINCT 刪除重復記錄

mysql教程 sql  distinct  刪除重復記錄

如何在mysql中使用distinct關鍵字過濾在一個select語句的結果列表重復值

如果你在查詢數據時想刪除重復的數據,可以利用distinct關鍵字過濾重復值,下面看下distinct用法。

select distinct column_name from table_name;

讓我們來看看一個簡單的例子,我們有一個汽車列表。每輛車都有一個id,品牌,類型和顏色,但現在我們只是簡單的列出所有品牌:

 

select brand from car;
the result is:audi                                                                                                                                    audi                                                                                                                                    audi                                                                                                                                    bmw                                                                                                                                    bmw                                                                                                                                    lexus                                                                                                                                    lexus    

                                                                                                         

正如你可以看到品牌顯示幾次。但是我們要的是知道有多少不同的品牌,在db的。要獲得此擴展查詢,如下所示

select distinct brand from car;
 the result is:
 audi                                                                                                       bmw
 lexus


 
如果你想知道有多少次不同的品牌,那麼我們就必須用count group by 來實現了,下面看實例


select brand, count(brand) from car group by brand;
and the result is:brand  count(brand)        
audi  3                   
bmw  2                   
lexus  2

 

copyright © 萬盛學電腦網 all rights reserved