Mysql-行列转换

https://blog.csdn.net/Hudas/article/details/124055641


DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
  `id` int(20) NOT NULL   COMMENT '主键 id',
  `name` varchar(50) DEFAULT NULL COMMENT '姓名',
  `course` varchar(50) DEFAULT NULL COMMENT '课程',
  `score` int(3) DEFAULT NULL COMMENT '成绩',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;
 
INSERT INTO `t_student` VALUES (1,'张三', '语文', 95);
INSERT INTO `t_student` VALUES (2,'李四', '语文', 99);
INSERT INTO `t_student` VALUES (3,'王五', '语文', 80);
INSERT INTO `t_student` VALUES (4,'张三', '数学', 86);
INSERT INTO `t_student` VALUES (5,'李四', '数学', 96);
INSERT INTO `t_student` VALUES (6,'王五', '数学', 81);
INSERT INTO `t_student` VALUES (7,'张三', '英语', 78);
INSERT INTO `t_student` VALUES (8,'李四', '英语', 88);
INSERT INTO `t_student` VALUES (9,'王五', '英语', 87);
INSERT INTO `t_student` VALUES (10,'张三', '历史', 98);
INSERT INTO `t_student` VALUES (11,'李四', '历史', 85);
INSERT INTO `t_student` VALUES (12,'王五', '历史', 89);


SELECT * from t_student ;
-- 列转行

SELECT name,
max(case  when course= "语文" then  score end) 语文,
max(case  when course ="数学" then  score  end) 数学,
max(case  when course ="英语" then  score end) 英语,
max(case  when course ="历史" then  score end) 历史
from t_student ts 
group by ts.name 

-- 行转列
SELECT t.name,"语文" as cource,t.语文 as score from (SELECT name,
													max(case  when course= "语文" then  score end) 语文,
													max(case  when course ="数学" then  score  end) 数学,
													max(case  when course ="英语" then  score end) 英语,
													max(case  when course ="历史" then  score end) 历史
													from t_student 
													group by name ) t
union all 
SELECT t.name,"数学" as cource,t.数学 as score from (SELECT name,
													max(case  when course= "语文" then  score end) 语文,
													max(case  when course ="数学" then  score  end) 数学,
													max(case  when course ="英语" then  score end) 英语,
													max(case  when course ="历史" then  score end) 历史
													from t_student 
													group by name ) t
union all 
SELECT t.name,"英语" as cource,t.英语 as score from (SELECT name,
													max(case  when course= "语文" then  score end) 语文,
													max(case  when course ="数学" then  score  end) 数学,
													max(case  when course ="英语" then  score end) 英语,
													max(case  when course ="历史" then  score end) 历史
													from t_student 
													group by name ) t
union all 
SELECT t.name,"历史" as cource,t.历史 as score from (SELECT name,
													max(case  when course= "语文" then  score end) 语文,
													max(case  when course ="数学" then  score  end) 数学,
													max(case  when course ="英语" then  score end) 英语,
													max(case  when course ="历史" then  score end) 历史
													from t_student 
													group by name ) t

-- 列转多行(字符串)

SELECT name,
group_concat(case  when course= "语文" then  score end) "语文",
group_concat(case  when course ="数学" then  score  end) "数学",
group_concat(case  when course ="英语" then  score end) "英语",
group_concat(case  when course ="历史" then  score end) "历史"
from t_student ts 
group by ts.name 

-- 字符串转换成列: 利用SUBSTRING_INDEX和mysql.help_topic实现
SELECT a.user_id,
       SUBSTRING_INDEX(SUBSTRING_INDEX(a.order_id, ',', b.help_topic_id + 1 ), ',',- 1 )AS order_id
FROM t_user_order AS a
LEFT JOIN mysql.help_topic AS b 
ON b.help_topic_id < (length(a.order_id) - length(REPLACE(a.order_id, ',', '' )) + 1);

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

@莫等闲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值