SQL SELECT DISTINCT Statement
Die SQL SELECT DISTINCT-stelling
Die SELECT DISTINCT
stelling word gebruik om slegs afsonderlike (verskillende) waardes terug te gee.
Binne 'n tabel bevat 'n kolom dikwels baie duplikaatwaardes; en soms wil jy net die verskillende (afsonderlike) waardes lys.
KIES DISTINCT Sintaksis
SELECT DISTINCT column1, column2, ...
FROM table_name;
Demo-databasis
Hieronder is 'n keuse uit die "Klante"-tabel in die Noordewind-voorbeelddatabasis:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 |
Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4 |
Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
KIES Voorbeeld sonder onderskeid
Die volgende SQL-stelling kies alle (insluitend die duplikate) waardes uit die "Land"-kolom in die "Klante"-tabel:
Voorbeeld
SELECT Country FROM Customers;
Kom ons gebruik nou die SELECT DISTINCT
stelling en sien die resultaat.
KIES DESINKE Voorbeelde
Die volgende SQL-stelling kies slegs die DISTINCT-waardes uit die "Country"-kolom in die "Customers"-tabel:
Voorbeeld
SELECT DISTINCT Country FROM Customers;
Die volgende SQL-stelling lys die aantal verskillende (afsonderlike) kliëntelande:
Voorbeeld
SELECT COUNT(DISTINCT Country) FROM Customers;
Let wel: Die voorbeeld hierbo sal nie in Firefox werk nie! Omdat COUNT(DISTINCT kolomnaam ) nie in Microsoft Access-databasisse ondersteun word nie. Firefox gebruik Microsoft Access in ons voorbeelde.
Hier is die oplossing vir MS Access:
Voorbeeld
SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Customers);