如何在 MySQL 中将行数据转置为列

Posted

技术标签:

【中文标题】如何在 MySQL 中将行数据转置为列【英文标题】:HOW TO TRANSPOSE ROWS DATA INTO COLUMN in MySQL 【发布时间】:2018-07-23 03:14:41 【问题描述】:

第一个问题

我运行这个简单的代码将行数据转换为列,但它只显示第一个数据。为什么会这样?

SELECT  
id_material,    
at_part_number,    
mat_desc,   
GROUP_CONCAT(equipment_name SEPARATOR ', ') As Equipment`

FROM material 

INNER JOIN equipment_list ON material.id_material=equipment_list.PKid_material           
INNER JOIN equipment ON equipment_list.PKid_equipment=equipment.id_equipment;

然后我该如何制作设备 1、设备 2、设备网 3、... 列?不是通过 SEPARATOR,而是创建一些新列?

--------------------------------------------------+    
| id_material | mat_part_number | Equipment    
+----------------------------------------------------+   
|     1000001 | AA111           | eq1, eq2, eq3 |  
+-----------------------------------------------------+   

第二个问题

SELECT  
id_material,    
mat_part_number,   
mat_desc,`   
GROUP_CONCAT(equipment_name SEPARATOR ', ') As Equipment

FROM material 

INNER JOIN equipment_list ON material.id_material=equipment_list.PKid_material INNER JOIN equipment ON equipment_list.PKid_equipment=equipment.id_equipment;

UNION

SELECT  
id_material,    
alt_part_number,   
mat_desc,   
GROUP_CONCAT(equipment_name SEPARATOR ', ') As Equipment 

FROM material 

INNER JOIN equipment_list ON material.id_material=equipment_list.PKid_material    
INNER JOIN equipment ON equipment_list.PKid_equipment=equipment.id_equipment      
INNER JOIN alternatif_list ONmaterial.id_material=alternatif_list.PKid_material    
INNER JOIN alternatif ON alternatif.id_alternatif=alternatif_list.PKid_alternatif

ORDER BY
id_material asc;

注意:材料是父表,材料表和设备表是多对多的关系,材料表和替代表也是如此。 1 材料可以包含 2 个设备,2 个替代(材料的设备等于替代的设备)

我想做这个

`---------------------------------------------------------------------------`   
`id_material | part_number | equipment (1) | equipment (2) | equipment (n) |`     
`---------------------------------------------------------------------------`  
`100001 | AA111 | nameeq1 |nameeq2 |`    
`100001 | AA112 | nameeq1 |nameeq2 |`    //alternatif part_number    
`100002 | AA222 | nameeq3 |    |`    
`100002 | AA221 | nameeq3 |    |`      //alternatif part_number       
`100003 | AA333 | nameeq4 |nameeq5    |`          
`100003 | AA331 | nameeq4 |nameeq5    |`     //alternatif part_number   

【问题讨论】:

考虑处理应用程序代码中的数据显示问题(如果有) Group_concat 是一个聚合函数,您需要一个 group by 子句。如果你想要单独的列,你需要动态 sql,并且应该搜索 mysql 行到列。 我刚刚读到它,我不明白... 不鼓励在帖子中提出多个问题;如果您想问两个问题,请发两个单独的帖子。 【参考方案1】:

鉴于如此简化的数据

DROP TABLE IF EXISTS T;
CREATE TABLE T (ID INT,col1 varchar(3));

insert into t values (1,'aaa'),(1,'bbb'),(2,'aaa'),(3,'aaa'),(3,'bbb'),(3,'ccc');

如果你 group_concat 没有 group by

select id,group_concat(col1)
from t;

你明白了

+------+-------------------------+
| id   | group_concat(col1)      |
+------+-------------------------+
|    1 | aaa,bbb,aaa,aaa,bbb,ccc |
+------+-------------------------+
1 row in set (0.00 sec)

按分组

select id,group_concat(col1)
from t
group by id;

你明白了

+------+--------------------+
| id   | group_concat(col1) |
+------+--------------------+
|    1 | aaa,bbb            |
|    2 | aaa                |
|    3 | aaa,bbb,ccc        |
+------+--------------------+
3 rows in set (0.00 sec)

如果您想要单独的列并且具有已知数量有限的值,您可以使用条件聚合

select id,
         max(case when col1 = 'aaa' then col1 else '' end) as first,
         max(case when col1 = 'bbb' then col1 else '' end) as second,
         max(case when col1 = 'ccc' then col1 else '' end) as third
from t
group by id;

得到这个

+------+-------+--------+-------+
| id   | first | second | third |
+------+-------+--------+-------+
|    1 | aaa   | bbb    |       |
|    2 | aaa   |        |       |
|    3 | aaa   | bbb    | ccc   |
+------+-------+--------+-------+
3 rows in set (0.00 sec)

如果您不知道这些值或者它们可能会发生变化,那么您可以构建一个 sql 语句并将其提交给动态 sql

set @sql = (
    select group_concat(concat('max(case when col1 = ', char(39),s.val,char(39),' then col1 else null end) as ', val))
    from
    (select distinct col1 as val from t order by col1) s
    )
;
set @sql = concat('select id,', @sql,' from t group by id;');   
prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;

返回这个

+------+------+------+------+
| id   | aaa  | bbb  | ccc  |
+------+------+------+------+
|    1 | aaa  | bbb  | NULL |
|    2 | aaa  | NULL | NULL |
|    3 | aaa  | bbb  | ccc  |
+------+------+------+------+
3 rows in set (0.00 sec)

如果你想限制列数,那么使用变量来计算最大列数将如下所示

DROP TABLE IF EXISTS T;
CREATE TABLE T (ID INT,col1 varchar(3));

insert into t values (1,'aaa'),(1,'bbb'),(2,'aaa'),(3,'aaa'),(3,'ddd'),(3,'eee');


set @sql = (
    select group_concat(distinct concat('max(case when s.rn = ', S.RN,' then col1 else null end) as Equipment_', S.RN))
    from
    (
        SELECT ID,
             IF(ID <> @P, @RN:=1,@RN:=@RN+1) RN,
            @P:=ID P
        FROM T
        CROSS JOIN (SELECT @RN:=0,@P:=0) R
    ) S
    );

set @sql = concat('select id,', @sql,' from 
(
        SELECT ID,col1,
             IF(ID <> @P, @RN:=1,@RN:=@RN+1) RN,
            @P:=ID P
        FROM T
        CROSS JOIN (SELECT @RN:=0,@P:=0) R
        order by id,col1
) S group by id;'); 

#SELECT @SQL;

prepare sqlstmt from @sql;
execute sqlstmt;
deallocate prepare sqlstmt;

+------+-------------+-------------+-------------+
| id   | Equipment_1 | Equipment_2 | Equipment_3 |
+------+-------------+-------------+-------------+
|    1 | aaa         | bbb         | NULL        |
|    2 | aaa         | NULL        | NULL        |
|    3 | aaa         | ddd         | eee         |
+------+-------------+-------------+-------------+
3 rows in set (0.00 sec)

【讨论】:

所以aaa、bbb等的栏目要限制? 如果我想要这样的列 id_material | part_number | equipment (1) | equipment (2) | equipment (n) | --------------------------------------------------------------------------- 100001 | AA111 | nameeq1 |nameeq2 | 100001 | AA112 | nameeq1 |nameeq2 | //alternatif part_number 100002 | AA222 | nameeq3 | | 100002 | AA221 | nameeq3 | | //alternatif part_number 100003 | AA333 | nameeq4 |nameeq5 | @98765433388备用零件号 我要设备的内容是免费的 @Sheena Mulankova 添加了 非常感谢!但什么是 S.RN、RN、P、R? @RN、@P、@R 有什么区别?

以上是关于如何在 MySQL 中将行数据转置为列的主要内容,如果未能解决你的问题,请参考以下文章

在 BigQuery 中将行转置为列

在 SQL 中将行转置为列标题

如何在红移中加入行并将其转置为列

将 SQL 行数据转置为列

Oracle SQL Developer:如何使用 PIVOT 函数将行转置为列

mysql 将行转置为列