SQL DISTINCT

查詢不重複值

DISTINCT 用於去除重複資料,常用在國家、城市、分類等欄位。它很適合先快速理解資料有哪些分類值。

Example 1 - 單欄位 DISTINCT

SELECT DISTINCT Country
FROM Customers;

Example 2 - 依城市去重複

SELECT DISTINCT City
FROM Customers
ORDER BY City;

Example 3 - 多欄位組合去重複

SELECT DISTINCT Country, City
FROM Customers
ORDER BY Country, City;

Example 4 - 計算不重複國家數

SELECT COUNT(DISTINCT Country) AS CountryCount
FROM Customers;