时间:2021-07-01 10:21:17 帮助过:2人阅读
drop table if exists t_score; create table t_score ( id int primary key auto_increment, name varchar(20), course varchar(20), score double ) default charset = utf8;
insert into `t_score`(name,course,score) values (‘王海‘, ‘语文‘, 86), (‘王海‘, ‘数学‘, 83), (‘王海‘, ‘英语‘, 93), (‘陶俊‘, ‘语文‘, 88), (‘陶俊‘, ‘数学‘, 84), (‘陶俊‘, ‘英语‘, 94), (‘刘可‘, ‘语文‘, 80), (‘刘可‘, ‘数学‘, 86), (‘刘可‘, ‘英语‘, 88), (‘李春‘, ‘语文‘, 89), (‘李春‘, ‘数学‘, 80), (‘李春‘, ‘英语‘, 87);
SELECT name, max(CASE course WHEN ‘语文‘ THEN score ELSE 0 END ) Chinese, max(CASE course WHEN ‘数学‘ THEN score ELSE 0 END ) math, max(CASE course WHEN ‘英语‘ THEN score ELSE 0 END ) English, sum(score) score FROM t_score GROUP BY name union select ‘TOTAL‘, sum(CASE course WHEN ‘语文‘ THEN score ELSE 0 END ), sum(CASE course WHEN ‘数学‘ THEN score ELSE 0 END ), sum(CASE course WHEN ‘英语‘ THEN score ELSE 0 END ), sum(score) from t_score;
效果图:
Mysql:case when then end 的用法
标签:函数 create lse select var 分享图片 def exists sele