SQL CASE 关键字
CASE
The CASE
command is used is to create different output based on conditions.
The following SQL goes through several conditions and returns a value when the specified condition is met
示例
SELECT OrderID, Quantity,
CASE
WHEN Quantity > 30 THEN '数量大于 30'
WHEN Quantity = 30 THEN '数量等于 30'
ELSE '数量小于 30'
END
FROM OrderDetails;
自己动手试一试 »
以下 SQL 语句将按城市对客户进行排序。但是,如果城市为 NULL,则按国家排序
示例
SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
WHEN City IS NULL THEN Country
ELSE City
END);
自己动手试一试 »