應用場景一:當更新Test表的時候,只對特定的字段更新,能夠採起下面的方法。下面的方法只對字段 BeginTime 爲 NULL 的 BeginTime 進行更新,BeginTime 不爲 NULL 的狀況不更新,並且其他字段 EndTime 也能夠同時更新。sql
UPDATE Test SET BeginTime = CASE WHEN BeginTime IS NULL THEN "2014-12-21 12:15:13" ELSE BeginTime END, EndTime = "2014-12-21 12:15:13"
應用場景二:SQL查詢時嵌入CASE WHEN。如判斷工資的等級,並統計每一等級的人數,能夠用下面的SQL語句。code
SELECT CASE WHEN salary <= 1500 THEN '1' WHEN salary > 1500 AND salary <= 2000 THEN '2' WHEN salary > 2000 AND salary <= 3000 THEN '3' WHEN salary > 3000 AND salary <= 4000 THEN '4' ELSE NULL END salary_class, COUNT(*) FROM Table_A GROUP BY CASE WHEN salary <= 1500 THEN '1' WHEN salary > 1500 AND salary <= 2000 THEN '2' WHEN salary > 2000 AND salary <= 3000 THEN '3' WHEN salary > 3000 AND salary <= 4000 THEN '4' ELSE NULL END;