如何将行数据转入MySQL中的列

时间:2018-07-23 03:14:41

标签: mysql sql pivot transpose

第一个问题

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

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”,“ Equipmnet 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   

1 个答案:

答案 0 :(得分:0)

给出这么简单的数据

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没有分组依据

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)