首先我们创建数据库表:
CREATE TABLE `t_demo` ( `id` int(32) NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int(2) DEFAULT NULL, `num` int(3) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
插入数据:
INSERT INTO `t_demo` VALUES (‘1‘, ‘张三‘, ‘21‘, ‘69‘); INSERT INTO `t_demo` VALUES (‘2‘, ‘李四‘, ‘22‘, ‘98‘); INSERT INTO `t_demo` VALUES (‘3‘, ‘王五‘, ‘20‘, ‘54‘); INSERT INTO `t_demo` VALUES (‘4‘, ‘赵甜‘, ‘22‘, ‘80‘);
MySQL case when 用法
SELECT *, CASE WHEN t.num >= 85 THEN ‘优秀‘ WHEN t.num < 90 AND t.num>= 60 THEN ‘一般‘ ELSE ‘不及格‘ END AS level FROM t_demo t;
查询结构:
?
原文地址:https://www.cnblogs.com/zhaixingzhu/p/12569288.html
时间: 2024-10-12 17:35:38