一般语法
CASE WHEN condition1 THEN result1
WHEN condition2 THEN result2
...
ELSE resultN
END
举个例子
SELECT CASE
WHEN age < 18 THEN '你好,小朋友'
WHEN age BETWEEN 18 AND 60 THEN '你好,先生/女士'
WHEN age > 60 THEN '你好,老先生/老太太'
ELSE "其他"
END AS greeting
FROM users;
如果我们要想把某个字段也一并输出到结果里头呢?
SELECT CASE
WHEN age < 18 THEN '你好,小朋友'
WHEN age BETWEEN 18 AND 60 THEN '你好,先生/女士'
WHEN age > 60 THEN '你好,老先生/老太太'
ELSE concat(age, '其他')
END AS greeting
FROM users;
用 concat(age, ‘其他’) 把某个字段的值和其他值连接也行