SQL SELECT
SELECT DISTINCT “欄位”,”欄位” FROM “表格名稱”; 我們用以下的例子來實作: 北風資料庫 (Northwind)
Customers 的資料表
SELECT * FROM Customers ;
CustomerID |
CompanyName |
ContactName |
Address |
City |
PostalCode |
Country |
ALFKI |
Alfreds Futterkiste |
Maria Anders |
Obere Str. 57 |
Berlin |
12209 |
Germany |
ANATR |
Ana Trujillo Emparedados y helados |
Ana Trujillo |
Avda. de la Constitucion 2222 |
Mexico D.F. |
05021 |
Mexico |
ANTON |
Antonio Moreno Taqueria |
Antonio Moreno |
Mataderos 2312 |
Mexico D.F. |
05023 |
Mexico |
AROUT |
Around the Horn |
Thomas Hardy |
120 Hanover Sq. |
London |
WA1 1DP |
UK |
BERGS |
Berglunds snabbkop |
Christina Berglund |
Berguvsvagen 8 |
Lulea |
S-958 22 |
Sweden |
BLAUS |
Blauer See Delikatessen |
Hanna Moos |
Forsterstr. 57 |
Mannheim |
68306 |
Germany |
BLONP |
Blondesddsl pere et fils |
Frederique Citeaux |
24, place Kleber |
Strasbourg |
67000 |
France |
BOLID |
Bolido Comidas preparadas |
Martin Sommer |
C/ Araquil, 67 |
Madrid |
28023 |
Spain |
BONAP |
Bon app' |
Laurence Lebihan |
12, rue des Bouchers |
Marseille |
13008 |
France |
BOTTM |
Bottom-Dollar Markets |
Elizabeth Lincoln |
23 Tsawassen Blvd. |
Tsawassen |
T2F 8M4 |
Canada |
選取從“Customers”表中的“Country”列中列出所有值:
SELECT Country FROM Customers ;
結果:
Country |
Germany |
Mexico |
Mexico |
UK |
Sweden |
Germany |
France |
Spain |
France |
Canada |
UK |
Argentina |
在結果資料中,例如Mexico , UK 被列出2次以上。 如果需要從“Customers”表中的“Country”列中選擇所有不同的值,我們就需要用 SELECT DISTINCT 語句
SELECT DISTINCT Country FROM Customers ;
結果:
Country |
Argentina |
Austria |
Belgium |
Brazil |
Canada |
Denmark |
Finland |
France |
Germany |
Ireland |
Italy |
Mexico |
Norway |
Poland |
Portugal |
Spain |
Sweden |
Switzerland |
UK |
USA |
Venezuela |
從表這兩個表中可以看到,重複的值不顯示出來。範例中的 Mexico、UK的資料被列出一次。