我们有三张桌子。
1) Product_Table
Product_ID
Product_Name
2) Warehouse1_Table
Product_ID
Inventory1
3) Warehouse2_Table
Product_ID
Inventory2
Warehouse1_Table或Warehouse2_Table中可能不存在产品ID 。
我正在尝试找出一个可以提供类似内容的查询:
Product_ID, Product_Name, TotalStock
如果Product_ID在两个表中,则TotalStock是Inventory1 + Inventory2。
如果Product_ID在Warehouse1_Table中,则TotalStock是Inventory1
如果Product_ID在Warehouse2_Table中,则TotalStock是Inventory2
如果Product_ID不在任何仓库表中,则TotalStock为0
答案 0 :(得分:2)
这样的事可能有用。我添加了一些示例数据来说明我的目标:
create table Product_table
(Product_ID int unsigned primary key,Product_Name varchar(50) not null,
UNIQUE KEY `Product_TableUIdx1` (Product_Name));
create table Warehouse1_Table
(Product_ID int unsigned not null,Inventory1 int,
UNIQUE KEY `Warehouse1_Table_UIdx1` (Product_Id));
create table Warehouse2_Table
(Product_ID int unsigned,Inventory2 int,
UNIQUE KEY `Warehouse2_Table_UIdx1` (Product_Id));
insert into Product_table values (1,"Banana");
insert into Product_table values (2,"Apple");
insert into Product_table values (3,"Pear");
insert into Product_table values (4,"Orange");
insert into Warehouse1_Table values (1,2);
insert into Warehouse2_Table values (3,10);
insert into Warehouse2_Table values (4,5);
insert into Warehouse2_Table values (1,3);
select pt.Product_ID,pt.Product_Name,
ifnull(wt1.Inventory1,0)+ifnull(wt2.Inventory2,0) as StockTotal
from Product_table pt
left outer join Warehouse1_Table wt1 on wt1.Product_ID = pt.Product_ID
left outer join Warehouse2_Table wt2 on wt2.Product_ID = pt.Product_ID;
感谢Dems的鹰眼指出,避免在Warehouse1_Table或Warehouse1_Table中为同一Product_ID的多行引起的重复是很重要的。如果无法像上面的DDL那样添加唯一键,那么您可以使用(有点丑陋)查询来解决这个问题:
select pt.Product_ID,pt.Product_Name,
ifnull(wt1.Inventory1Total,0)+ifnull(wt2.Inventory2Total,0) as StockTotal
from Product_table pt
left outer join (select w1.Product_ID,sum(w1.Inventory1) as Inventory1Total
from Warehouse1_Table w1 group by w1.Product_ID) as wt1 on wt1.Product_ID = pt.Product_ID
left outer join (select w2.Product_ID,sum(w2.Inventory2) as Inventory2Total
from Warehouse2_Table w2 group by w2.Product_ID) as wt2 on wt2.Product_ID = pt.Product_ID;
答案 1 :(得分:1)
尝试使用LEFT JOIN,如果您的交叉表没有产生匹配,那么这是一个不会破坏您的查询的连接。然后总结一下清单,如下:
SELECT p.Product_ID, p.Product_Name,
(IFNULL(w1.Inventory1, 0) + ISNULL(w2.Inventory2, 0)) AS TotalStock
FROM `Product_Table` AS p
LEFT JOIN `Warehouse1_Table` AS w1 ON (
p.Product_ID = w1.Product_ID
) LEFT JOIN `Warehouse2_Table` AS w2 ON (
p.Product_ID = w2.Product_ID
);
然后,您的结果集应包含“TotalStock”字段,其中包含组合库存或其中一个仓库返回的库存。
答案 2 :(得分:0)
SELECT Product_ID,
Product_Name,
SUM(w1.Inventory1) as sum1,
SUM(w2.Inventroy2) as sum2
FROM Product_Table p
LEFT JOIN Warehouse1_Table w1 ON p.Product_ID = w1.Product_ID
LEFT JOIN Warehouse2_Table w2 ON p.Product_ID = w2.Product_ID
GROUP BY p.Product_ID
我没有测试过,但这就是想法。
答案 3 :(得分:0)
SELECT Product_ID, Product_Name,
Inventory1 + Inventory2 AS TotalStock
FROM Product_Table
NATURAL JOIN Warehouse1_Table
NATURAL JOIN Warehouse2_Table
UNION
SELECT Product_ID, Product_Name,
Inventory1 AS TotalStock
FROM Product_Table
NATURAL JOIN Warehouse1_Table
WHERE Product_ID NOT IN (
SELECT Product_ID
FROM Warehouse2_Table
)
UNION
SELECT Product_ID, Product_Name,
Inventory2 AS TotalStock
FROM Product_Table
NATURAL JOIN Warehouse2_Table
WHERE Product_ID NOT IN (
SELECT Product_ID
FROM Warehouse1_Table
)
UNION
SELECT Product_ID, Product_Name,
0 AS TotalStock
FROM Product_Table
WHERE Product_ID NOT IN (
SELECT Product_ID
FROM Warehouse1_Table
)
AND Product_ID NOT IN (
SELECT Product_ID
FROM Warehouse2_Table
);