Parola chiave SQL CASE
❮ Riferimento alle parole chiave SQL
ASTUCCIO
Il CASE
comando viene utilizzato per creare output diversi in base alle condizioni.
Il seguente SQL passa attraverso diverse condizioni e restituisce un valore quando viene soddisfatta la condizione specificata:
Esempio
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30
THEN "The quantity is greater than 30"
WHEN Quantity =
30 THEN "The quantity is 30"
ELSE "The quantity is
under 30"
END
FROM OrderDetails;
Il seguente SQL ordinerà i clienti per città. Tuttavia, se Città è NULL, ordina per Paese:
Esempio
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
❮ Riferimento alle parole chiave SQL