SQL行列转换问题

1、列转行

  • 建表语句
DROP TABLE IF EXISTS `t_student`;
CREATE TABLE `t_student` (
  `id` int(20) NOT NULL AUTO_INCREMENT 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,'张三', '语文', 99);
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,'王五', '英语', 88);
INSERT INTO `t_student` VALUES (10,'张三', '历史', 98);
INSERT INTO `t_student` VALUES (11,'李四', '历史', 85);
INSERT INTO `t_student` VALUES (12,'王五', '历史', 89);
INSERT INTO `t_student` VALUES ('13', '赵六', '语文', '90');
INSERT INTO `t_student` VALUES ('14', '赵六', '数学', '80');
INSERT INTO `t_student` VALUES ('15', '赵六', '英语', '60');
INSERT INTO `t_student` VALUES ('16', '赵六', '历史', '82');

在这里插入图片描述

  • max(case when) as ’ ’
select
	name,
    max(case when course='语文' then score end) as '语文',
    max(case when course='数学' then score end) as '数学',
    max(case when course='英语' then score end) as '英语',
    max(case when course='历史' then score end) as '历史'
from t_student
group by name;

在这里插入图片描述

2、行转列

  • 建表语句
DROP TABLE IF EXISTS `t_course`;
CREATE TABLE `t_course` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_name` varchar(50) DEFAULT NULL COMMENT '用户名',
  `chinese` double DEFAULT NULL COMMENT '语文成绩',
  `math` double DEFAULT NULL COMMENT '数学成绩',
  `english` double DEFAULT NULL COMMENT '英语成绩',
  `history` double DEFAULT NULL COMMENT '历史成绩',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

INSERT INTO t_course VALUES ('1', '张三', '100', '86', '98', '98');
INSERT INTO t_course VALUES ('2', '李四', '99', '99', '88', '65');
INSERT INTO t_course VALUES ('3', '王五', '80', '85', '80', '89');

在这里插入图片描述

  • union 或者 union all
select
	user_name as name,'语文' as course,chinese as score
from t_course
union all
select
	user_name as name,'数学' as course,math as score
from t_course
union all
select
	user_name as name,'英语' as course,english as score
from t_course
union all
select
	user_name as name,'历史' as course,history as score
from t_course;

在这里插入图片描述

3、列转字符串 group_concat()

select
	name,
    group_concat(course,':',score)
from t_student
group by name;

在这里插入图片描述

4、字符串转列

  • 建表语句

DROP TABLE IF EXISTS `t_user_order`;
CREATE TABLE `t_user_order` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键 id',
  `user_id` varchar(50) DEFAULT NULL COMMENT '用户 id',
  `order_id` varchar(100) DEFAULT NULL COMMENT '订单 ids',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
INSERT INTO t_user_order VALUES ('1', 'user1', '1,4,7,10,13');
INSERT INTO t_user_order VALUES ('2', 'user2', '2,5,8,11,14');
INSERT INTO t_user_order VALUES ('3', 'user3', '3,6,9,12,15');

在这里插入图片描述

  • SUBSTRING_INDEX()
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 );

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值