gpt4 book ai didi

mysql - SQL 一行一对多查询

转载 作者:行者123 更新时间:2023-11-29 16:18:34 25 4
gpt4 key购买 nike

我有一个包含产品的表、一个包含四个制造商的表和一个中间表。
我想导出一份产品列表,这些产品可能有多个制造商,也可能没有多个制造商。
我想将每个产品导出为一行,右侧有四列(每个制造商一列)。

产品表

|----|------|
| id | name |
|----|------|
| 12 | foo |
|----|------|

产品制造商表

|----|------------|------------------|---------------------------|
| id | product_id | manufacturer_id | manufacturer_product_code |
|----|------------|------------------|---------------------------|
| XX | 12 | ABCD | X1X2 |
|----|------------|------------------|---------------------------|
| YY | 12 | LMKO | AAAB |
|----|------------|------------------|---------------------------|

期望的结果:

+------+-------------+--------------------+--------------------+--------------------+--------------------+
| name | internal_id | manufacturer1_code | manufacturer2_code | manufacturer3_code | manufacturer4_code |
+------+-------------+--------------------+--------------------+--------------------+--------------------+
| foo | 12 | X1X2 | null | AAAB | null |
+------+-------------+--------------------+--------------------+--------------------+--------------------+

我已经尝试过这个:

     SELECT p.`name` AS product, p.`id` AS internal_id, 
(
SELECT pm.`manufacturer_product_code`
FROM `product_manufacturer` pm
WHERE pm.`manufacturer_id` internal_id
AND pm.`manufacturer_id` LIKE 'ABCD'
) AS manufacturer1_code,
(
SELECT pm.`manufacturer_product_code`
FROM `product_manufacturer` pm
WHERE pm.`manufacturer_id` LIKE internal_id
AND pm.`manufacturer_id` LIKE 'CDCD'
) AS manufacturer2_code,
(
SELECT pm.`manufacturer_product_code`
FROM `product_manufacturer` pm
WHERE pm.`manufacturer_id` LIKE internal_id
AND pm.`manufacturer_id` LIKE 'LMKO'
) AS manufacturer3_code,
(
SELECT pm.`manufacturer_product_code`
FROM `product_manufacturer` pm
WHERE pm.`manufacturer_id` LIKE internal_id
AND pm.`manufacturer_id` LIKE 'RSRS'
) AS manufacturer4_code
FROM `product_manufacturer` pm
INNER JOIN `products` p
ON p.`id`=pm.`product_id`

这很好地返回了前两列,但其他四列返回 null。
当我让它工作时,我应该使用 Group by p.name

正确的做法是什么?谢谢。

最佳答案

实现此目的的一种方法是通过 row_number 模拟

DROP TABLE IF EXISTS T,T1;
CREATE TABLE T (ID INT, NAME VARCHAR(10));
CREATE TABLE T1( id varchar(2), product_id int, manufacturer_id varchar(20), manufacturer_product_code varchar(20));

insert into t values(12,'foo');
insert into t1 values
('XX' , 12 , 'ABCD' , 'X1X2'),
('YY' , 12 , 'LMKO' , 'AAAB');

select t.name,t.id,
max(case when rownumber = 1 then manufacturer_product_code else '' end) man1,
max(case when rownumber = 2 then manufacturer_product_code else '' end) man2,
max(case when rownumber = 3 then manufacturer_product_code else '' end) man3,
max(case when rownumber = 4 then manufacturer_product_code else '' end) man4
from t
join
(
select t1.*,
if(t1.product_id <> @p,@rn:=1,@rn:=@rn+1) rownumber,
@p:=t1.product_id p
from t1
cross join (select @rn:=0,@p:=0) r
order by t1.product_id,t1.id
) s
on s.product_id = t.id
group by t.name,t.id;

在此查询中,在子查询中分配行号,并在外部查询中使用条件聚合将制造商分配到列。

+------+------+------+------+------+------+
| name | id | man1 | man2 | man3 | man4 |
+------+------+------+------+------+------+
| foo | 12 | X1X2 | AAAB | | |
+------+------+------+------+------+------+
1 row in set (0.00 sec)

如果您有版本 8 或更高版本,您可以使用 row_number 窗口函数,而不是使用子查询中的变量进行模拟。

关于mysql - SQL 一行一对多查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54646422/

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