求列转行写法

MySql 码拜 7年前 (2017-05-03) 1635次浏览
表内容:
id        name         xk
1           A              学科1
2           A              学科2
3           A              学科3
4           B              学科2
5           B              学科3
希望转成
name         xk                     xk                     xk
A               学科1              学科2              学科3
B               学科2              学科3
原因是学科数量不定,所以不能用case when then判断学科名
解决方案

10

转完的列有几列?规则最好说一下。三个xk 列,然后第一个A是学科1 B是学科2

10

http://blog.csdn.net/acmain_chm/article/details/4283943
MySQL交叉表
在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义。http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html?96198现整理解法如下:数据样本: create table tx(  id int primary key,  c1 c…

10

假如学科不固定,那就得用动态行转列的方法了。
给你一个行转列的例子:

mysql 行列转换
 A 表 
  id          time       date
 1        7:00     20160105
 1        8:00     20160105
 1        7:00     20160104
 2        7:00     20160104
转换后的期望结果
 id     time1   time2    date
 1        7:00                  20160104
 2        7:00                  20160104 
 1       7:00    8:00       20160105
 --1.建表
 drop table if exists t_time;
CREATE TABLE `t_time` (
`id`  int(11) ,
`time`  varchar(10) ,
`date`  varchar(10)   
);
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("1", "7:00", "20160105");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("1", "8:00", "20160105");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("1", "7:00", "20160104");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("2", "7:00", "20160104");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("2", "9:00", "20160104");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("3", "7:00", "20160104");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("3", "8:00", "20160104");
INSERT INTO `test`.`t_time` (`id`, `time`, `date`) VALUES ("3", "9:00", "20160104");
select id,
	   SUBSTR(a,1,4) time1,
	   if(SUBSTR(a,5,4)="8:00",SUBSTR(a,5,4),null) time2,
	   if(SUBSTR(a,5,4)="9:00",SUBSTR(a,5,4),SUBSTR(a,9,4))  time3,
	   date
  from (select id,date, replace(GROUP_CONCAT(time),",","") as a 
		  from t_time a 
		 group by id,date
       ) b;
--2.生成动态sql
set @sql = "";
select @sql := concat(@sql,",max(case when time = """,time,""" then time end)") from (select distinct time from t_time)t;
set @sql = concat("select id",@sql,",date from t_time group by id,date");
prepare stmt from @sql;
execute stmt;
--3.执行结果
mysql> set @sql = "";
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> select @sql := concat(@sql,",max(case when time = """,time,""" then time end)") from (select distinct time from t_time)t;
+--+
| @sql := concat(@sql,",max(case when time = """,time,""" then time end)")                                                          |
+--+
| ,max(case when time = "7:00" then time end)                                                                                       |
| ,max(case when time = "7:00" then time end),max(case when time = "8:00" then time end)                                            |
| ,max(case when time = "7:00" then time end),max(case when time = "8:00" then time end),max(case when time = "9:00" then time end) |
+--+
3 rows in set (0.03 sec)
mysql>
mysql> set @sql = concat("select id",@sql,",date from t_time group by id,date");
Query OK, 0 rows affected (0.01 sec)
mysql>
mysql> prepare stmt from @sql;
Query OK, 0 rows affected (0.00 sec)
Statement prepared
mysql>
mysql> execute stmt;
+--+--+--+--+--+
| id   | max(case when time = "7:00" then time end) | max(case when time = "8:00" then time end) | max(case when time = "9:00" then time end) | date     |
+--+--+--+--+--+
|    1 | 7:00                                       | NULL                                       | NULL                                       | 20160104 |
|    1 | 7:00                                       | 8:00                                       | NULL                                       | 20160105 |
|    2 | 7:00                                       | NULL                                       | 9:00                                       | 20160104 |
|    3 | 7:00                                       | 8:00                                       | 9:00                                       | 20160104 |
+--+--+--+--+--+
4 rows in set (0.00 sec)

10

引用:
SELECT a.tutor_id, a.xm,
GROUP_CONCAT(a.recr_subj_id),
GROUP_CONCAT(a.recr_subj_name)
FROM `v_tutor_recr_subj` a
GROUP BY a.xm
ORDER BY a.tutor_id;

这种写法比较接近本人的需求,但它把多列合成一列了,有没有能分开的写法?

不能单独用1个SQL ,满足不了。
本人做的项目中,也有这类的需求。
使用的是存储过程,返回1个DATESET。 包含2个DATETABLE 结果集。
1个是学生的结果集,一个是对应学科的结果集。
然后在客户端处理。
客户端先画出最大学科列。 例如这些学生中,不同学科总共有3个(画三列,学科1,学科2,学科3)。
然后再循环结果集1(学生)中,依次取结果集2(学科),进行绑定值。
例如学生A   学科1   学科2
学生B    学科1                 学科3
学生C                  学科2    学科3
最后再绑定界面


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明求列转行写法
喜欢 (0)
[1034331897@qq.com]
分享 (0)