CASE WHEN的用法
1.case when语法在SQL中,“Case When”语句用于选择判断,在执行时先对条件进行判断,然后根据判断结果做出相应的操作;格式CASE SCORE WHEN 'A' THEN '优' ELSE '不及格' END2.适用场景2.1 表结构和数据SET NAMES utf8mb4;SET FOREIGN_KEY_CHECKS = 0;-- -----------------------
·
1.case when语法
- 在SQL中,“Case When”语句用于选择判断,在执行时先对条件进行判断,然后根据判断结果做出相应的操作;
- 格式
CASE SCORE WHEN 'A' THEN '优' ELSE '不及格' END
2.适用场景
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`studentId` int(9) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`studentName` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '姓名',
`grade` varchar(4) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '年级',
`major` varchar(20) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '专业',
`clazz` varchar(10) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '班级',
`institute` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '学院',
`tel` varchar(11) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '电话号码',
`email` varchar(30) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '电子邮件',
`pwd` varchar(16) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '密码',
`cardId` varchar(18) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '身份证号',
`sex` varchar(2) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL COMMENT '性别',
`role` varchar(1) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT '2' COMMENT '角色(0管理员,1教师,2学生)',
PRIMARY KEY (`studentId`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 20172670 CHARACTER SET = utf8 COLLATE = utf8_bin COMMENT = '学生信息表' ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (20154001, '隔壁老王', '2015', '计算机科学与技术', '2', '软件工程学院', '13585439532', 'gblw@163.com', '123456', '3412312', '男', '2');
INSERT INTO `student` VALUES (20154084, '大咸鱼', '2015', '计算机科学与技术', '2', '软件工程学院', '13658377857', '13658377857@sina.cn', '123456', '124123124535', '男', '2');
INSERT INTO `student` VALUES (20155003, '张大宝', '2015', '信息工程', '1', '软件工程学院', '15583829425', '15583829425@163.com', '123456', '3242342', '男', '2');
INSERT INTO `student` VALUES (20155007, '翠花', '2015', '网络工程', '1', '软件工程学院', '18734538457', '18734538457@163.com', '123456', '2423423', '女', '2');
INSERT INTO `student` VALUES (20155008, '小王', '2015', '信息工程', '2', '软件工程学院', '15523619564', '15523619564@163.com', '123456', '500234199704022353', '男', '2');
INSERT INTO `student` VALUES (20172147, '阿联', '2017', '移动终端软件开发', '2', '软件工程学院', '13576217034', '201726702147@jxnu.edu.cn', '123456', '20172670214', '男', '2');
SET FOREIGN_KEY_CHECKS = 1;
select studentId,
(CASE grade WHEN 2015 THEN '学长' ELSE '学弟' END) as 同届
from student;
select
sum(case grade when 2015 then 1 else 0 end) as '2015届',
sum(case grade when 2017 then 1 else 0 end) as '2017届'
from student;
更多推荐
已为社区贡献1条内容
所有评论(0)