gpt4 book ai didi

mysql - 如何组合第一个和第二个 Select SUM 语句

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

第一个 select 语句在计划时检索总和项目数据,第二个 select 语句在实际执行时检索总和项目数据。如何在不影响第一个 SQL 语句结果的情况下运行第二个 SQL 语句

这是 SQL fiddle :Fiddle

第一个 SQL 选择

SELECT
p.Content_Number AS PartNumber,
p.Content_Name AS PartName,
SUM(IF(p.ID_Packing IN(
SELECT
pck0.ID_Packing
FROM
tb_packing pck0
WHERE
pck0.ID_Project_Item = 1
), p.Quantity, 0)) AS Quantity
FROM tb_packing_plan p
WHERE
p.ID_Packing IN(
SELECT pck.ID_Packing
FROM tb_packing pck
WHERE pck.ID_Project_Item = 1
)
GROUP BY p.Content_Number
ORDER BY p.Content_Number;

第二个 SQL 选择

SELECT DISTINCT
p.Content_Number AS PartNumber,
p.Content_Name AS PartName,
SUM(IF(r.ID_Packing IN(
SELECT pck1.ID_Packing
FROM tb_packing pck1
WHERE pck1.ID_Project_Item = 1
), r.Quantity, 0)) AS Quantity2
FROM
tb_packing_plan p
LEFT JOIN tb_packing_real r
ON r.Content_Number = p.Content_Number
WHERE
p.ID_Packing IN(
SELECT pck.ID_Packing
FROM tb_packing pck
WHERE pck.ID_Project_Item = 1
)
GROUP BY p.ID_Packing,
p.Content_Number
ORDER BY p.Content_Number;

预期结果

|       PartNumber       |            PartName           | Quantity | Quantity2 |
| 1212121212 | Lorem Ipsum Dolor Sit Amet 12 | 12 | 6 |
| 1313131313 | Lorem Ipsum Dolor Sit Amet 13 | 2 | 2 |
| 1414141414 | Lorem Ipsum Dolor Sit Amet 14 | 6 | 5 |
| 1515151515 | Lorem Ipsum Dolor Sit Amet 15 | 2 | 0 |
| 2626262626 | Lorem Ipsum Dolor Sit Amet 26 | 9 | 6 |
| 2727272727 | Lorem Ipsum Dolor Sit Amet 27 | 12 | 11 |
| 3131313131 | Lorem Ipsum Dolor Sit Amet 31 | 5 | 0 |
| 3232323232 | Lorem Ipsum Dolor Sit Amet 32 | 4 | 4 |
| 3535353535 | Lorem Ipsum Dolor Sit Amet 35 | 5 | 5 |
| 3636363636 | Lorem Ipsum Dolor Sit Amet 36 | 2 | 0 |
| 3737373737 | Lorem Ipsum Dolor Sit Amet 37 | 5 | 0 |
| 3838383838 | Lorem Ipsum Dolor Sit Amet 38 | 8 | 3 |
| 4141414141 | Lorem Ipsum Dolor Sit Amet 41 | 7 | 0 |
| 4343434343 | Lorem Ipsum Dolor Sit Amet 43 | 3 | 0 |
| 4545454545 | Lorem Ipsum Dolor Sit Amet 45 | 3 | 0 |
| 4646464646 | Lorem Ipsum Dolor Sit Amet 46 | 3 | 0 |
| KND02S-D0001-0907-A7072| Manufacturing Part KND02S | 5 | 5 |

最佳答案

简单地加入两个查询

SELECT A.PartNumber,A.Partname,A.Quantity,B.Quantity2 FROM 
(
SELECT p.Content_Number AS PartNumber, p.Content_Name AS PartName, SUM(IF(p.ID_Packing IN (SELECT pck0.ID_Packing FROM tb_packing pck0 WHERE pck0.ID_Project_Item = 1), p.Quantity, 0)) AS Quantity
FROM tb_packing_plan p
WHERE p.ID_Packing IN (SELECT pck.ID_Packing FROM tb_packing pck WHERE pck.ID_Project_Item = 1)
GROUP BY p.Content_Number
ORDER BY p.Content_Number
)A JOIN
(
SELECT DISTINCT p.Content_Number AS PartNumber, p.Content_Name AS PartName, SUM(IF(r.ID_Packing IN (SELECT pck1.ID_Packing FROM tb_packing pck1 WHERE pck1.ID_Project_Item = 1), r.Quantity, 0)) AS Quantity2
FROM tb_packing_plan p LEFT JOIN tb_packing_real r ON r.Content_Number = p.Content_Number
WHERE p.ID_Packing IN (SELECT pck.ID_Packing FROM tb_packing pck WHERE pck.ID_Project_Item = 1)
GROUP BY p.ID_Packing ,p.Content_Number
ORDER BY p.Content_Number
) B ON A.PartNumber = B.PartNumber

SQL Fiddle

关于mysql - 如何组合第一个和第二个 Select SUM 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51978310/

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