赞
踩
1. 简单函数
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END
2. 搜索函数
CASE WHEN [expr] THEN [result1]…ELSE [default] END
CASE [col_name] WHEN [value1] THEN [result1]…ELSE [default] END:
枚举这个字段所有可能的值*
SELECT
stu_name AS '英雄',(
CASE stu_name
WHEN '赵信' THEN '长枪'
WHEN '皇子' THEN '方天画戟'
WHEN '寒冰' THEN '弩'
WHEN '蛮王' THEN '大刀'
ELSE '无'
END
) AS '装备'
FROM edu_student
CASE WHEN [expr] THEN [result1]…ELSE [default] END:
搜索函数可以写判断,并且搜索函数只会返回第一个符合条件的值,其他case被忽略
SELECT
stu_name AS '英雄',stu_age AS '年龄',(
CASE
WHEN stu_age < 18 THEN '少年'
WHEN stu_age BETWEEN 18 AND 30 THEN '青年'
WHEN stu_age >30 AND stu_age < 50 THEN '中年'
ELSE '老年'
END
) AS '状态'
FROM edu_student
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。