gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 17:17:32 24 4
gpt4 key购买 nike

第一个问题

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

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;

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

我想做这个

`---------------------------------------------------------------------------`   
`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

最佳答案

考虑到如此简单的数据

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 by的情况下进行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)

关于mysql - 如何在 MySQL 中将行数据转置为列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51471163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com