在本教學中,您將學習如何使用MySQL DISTINCT
子句與SELECT
語句一起組合來消除結果集中的重複行。
從表中查詢資料時,可能會收到重複的行記錄。為了刪除這些重複行,可以在SELECT
語句中使用DISTINCT
子句。
DISTINCT
子句的語法如下:
SELECT DISTINCT
columns
FROM
table_name
WHERE
where_conditions;
下面來看看一個使用DISTINCT
子句從employees
表中選擇員工的唯一姓氏(lastName
)的簡單範例。
首先,使用SELECT
語句從employees
表中查詢員工的姓氏(lastName
),如下所示:
SELECT
lastname
FROM
employees
ORDER BY lastname;
執行上面查詢語句,得到以下結果 -
mysql> SELECT lastname FROM employees ORDER BY lastname;
+-----------+
| lastname |
+-----------+
| Bondur |
| Bondur |
| Bott |
| Bow |
| Castillo |
| Firrelli |
| Firrelli |
| Fixter |
| Gerard |
| Hernandez |
| Jennings |
| Jones |
| Kato |
| King |
| Marsh |
| Murphy |
| Nishi |
| Patterson |
| Patterson |
| Patterson |
| Thompson |
| Tseng |
| Vanauf |
+-----------+
23 rows in set
可看到上面結果中,有好些結果是重複的,比如:Bondur
,Firrelli
等,那如何做到相同的結果只顯示一個呢?要刪除重複的姓氏,請將DISTINCT
子句新增到SELECT
語句中,如下所示:
SELECT DISTINCT
lastname
FROM
employees
ORDER BY lastname;
執行上面查詢,得到以下輸出結果 -
mysql> SELECT DISTINCT lastname FROM employees ORDER BY lastname;
+-----------+
| lastname |
+-----------+
| Bondur |
| Bott |
| Bow |
| Castillo |
| Firrelli |
| Fixter |
| Gerard |
| Hernandez |
| Jennings |
| Jones |
| Kato |
| King |
| Marsh |
| Murphy |
| Nishi |
| Patterson |
| Thompson |
| Tseng |
| Vanauf |
+-----------+
19 rows in set
當使用DISTINCT
子句時,重複的姓氏(lastname
)在結果集中被消除。
如果列具有NULL
值,並且對該列使用DISTINCT
子句,MySQL將保留一個NULL
值,並刪除其它的NULL
值,因為DISTINCT
子句將所有NULL
值視為相同的值。
例如,在customers
表中,有很多行的州(state
)列是NULL
值。 當使用DISTINCT
子句來查詢客戶所在的州時,我們將看到唯一的州和NULL
值,如下查詢所示:
SELECT DISTINCT
state
FROM
customers;
執行上面查詢語句後,輸出結果如下 -
mysql> SELECT DISTINCT state FROM customers;
+---------------+
| state |
+---------------+
| NULL |
| NV |
| Victoria |
| CA |
| NY |
| PA |
| CT |
| MA |
| Osaka |
| BC |
| Qubec |
| Isle of Wight |
| NSW |
| NJ |
| Queensland |
| Co. Cork |
| Pretoria |
| NH |
| Tokyo |
+---------------+
19 rows in set
可以使用具有多個列的DISTINCT
子句。 在這種情況下,MySQL使用所有列的組合來確定結果集中行的唯一性。
例如,要從customers
表中獲取城市(city
)和州(state
)的唯一組合,可以使用以下查詢:
SELECT DISTINCT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY state , city;
執行上面查詢,得到以下結果 -
mysql> SELECT DISTINCT state, city FROM customers WHERE state IS NOT NULL ORDER BY state ,city;
+---------------+----------------+
| state | city |
+---------------+----------------+
| BC | Tsawassen |
| BC | Vancouver |
| CA | Brisbane |
| CA | Burbank |
| CA | Burlingame |
| CA | Glendale |
| CA | Los Angeles |
| CA | Pasadena |
| CA | San Diego |
| CA | San Francisco |
| CA | San Jose |
| CA | San Rafael |
| Co. Cork | Cork |
| CT | Bridgewater |
| CT | Glendale |
| CT | New Haven |
| Isle of Wight | Cowes |
| MA | Boston |
| MA | Brickhaven |
| MA | Cambridge |
| MA | New Bedford |
| NH | Nashua |
| NJ | Newark |
| NSW | Chatswood |
| NSW | North Sydney |
| NV | Las Vegas |
| NY | NYC |
| NY | White Plains |
| Osaka | Kita-ku |
| PA | Allentown |
| PA | Philadelphia |
| Pretoria | Hatfield |
| Qubec | Montral |
| Queensland | South Brisbane |
| Tokyo | Minato-ku |
| Victoria | Glen Waverly |
| Victoria | Melbourne |
+---------------+----------------+
37 rows in set
沒有DISTINCT
子句,將查詢獲得州(state
)和城市(city
)的重複組合如下:
SELECT
state, city
FROM
customers
WHERE
state IS NOT NULL
ORDER BY state , city;
執行上面查詢,得到以下結果 -
如果在SELECT
語句中使用GROUP BY
子句,而不使用聚合函式,則GROUP BY
子句的行為與DISTINCT
子句類似。
以下語句使用GROUP BY
子句來選擇customers
表中客戶的唯一state
列的值。
SELECT
state
FROM
customers
GROUP BY state;
執行上面查詢,得到以下結果 -
mysql> SELECT state FROM customers GROUP BY state;
+---------------+
| state |
+---------------+
| NULL |
| BC |
| CA |
| Co. Cork |
| CT |
| Isle of Wight |
| MA |
| NH |
| NJ |
| NSW |
| NV |
| NY |
| Osaka |
| PA |
| Pretoria |
| Qubec |
| Queensland |
| Tokyo |
| Victoria |
+---------------+
19 rows in set
可以通過使用DISTINCT
子句來實現類似的結果:
mysql> SELECT DISTINCT state FROM customers;
+---------------+
| state |
+---------------+
| NULL |
| NV |
| Victoria |
| CA |
| NY |
| PA |
| CT |
| MA |
| Osaka |
| BC |
| Qubec |
| Isle of Wight |
| NSW |
| NJ |
| Queensland |
| Co. Cork |
| Pretoria |
| NH |
| Tokyo |
+---------------+
19 rows in set
一般而言,DISTINCT
子句是GROUP BY
子句的特殊情況。 DISTINCT
子句和GROUP BY
子句之間的區別是GROUP BY
子句可對結果集進行排序,而DISTINCT
子句不進行排序。
如果將ORDER BY子句新增到使用DISTINCT
子句的語句中,則結果集將被排序,並且與使用GROUP BY
子句的語句返回的結果集相同。
SELECT DISTINCT
state
FROM
customers
ORDER BY state;
執行上面查詢,得到以下結果 -
mysql> SELECT DISTINCT state FROM customers ORDER BY state;
+---------------+
| state |
+---------------+
| NULL |
| BC |
| CA |
| Co. Cork |
| CT |
| Isle of Wight |
| MA |
| NH |
| NJ |
| NSW |
| NV |
| NY |
| Osaka |
| PA |
| Pretoria |
| Qubec |
| Queensland |
| Tokyo |
| Victoria |
+---------------+
19 rows in set
可以使用具有聚合函式(例如SUM,AVG和COUNT)的DISTINCT
子句中,在MySQL將聚合函式應用於結果集之前刪除重複的行。
例如,要計算美國客戶的唯一state
列的值,可以使用以下查詢:
SELECT
COUNT(DISTINCT state)
FROM
customers
WHERE
country = 'USA';
執行上面查詢,得到以下結果 -
mysql> SELECT COUNT(DISTINCT state) FROM customers WHERE country = 'USA';
+-----------------------+
| COUNT(DISTINCT state) |
+-----------------------+
| 8 |
+-----------------------+
1 row in set
如果要將DISTINCT
子句與LIMIT子句一起使用,MySQL會在查詢LIMIT
子句中指定的唯一行數時立即停止搜尋。
以下查詢customers
表中的前3
個非空(NOT NULL)唯一state
列的值。
mysql> SELECT DISTINCT state FROM customers WHERE state IS NOT NULL LIMIT 3;
+----------+
| state |
+----------+
| NV |
| Victoria |
| CA |
+----------+
3 rows in set
在本教學中,我們學習了使用MySQL DISTINCT
子句的各種方法,例如消除重複行和計數非NULL
值。