我有一个[产品]表,
ProductID | Productname
1 | Bike
2 | Car
还有一个表[Stores],其中有2个不同的仓库的数量,第一个仓库是1,第二个仓库是2。
表[商店]包含产品的ID
ID |ProductID | Qty |wharehouse
1 |1 | 10 |1
2 |1 | 5 |2
3 |2 | 6 |1
4 |2 | 7 |2
我要查询以显示在每个产品的一条记录中,以下内容
ProductID,ProductName,QtyFromFirstWhareHouse,QtyFromSecondWhareHouse
我尝试对每个数量进行第二次查询,但是我不输入什么标准。谢谢
你能帮助我吗?非常感谢
关联子查询可能是最简单的解决方案:
select p.*,
(select s.qty
from stores as s
where s.productid = p.productid and s.warehouse = 1
) as quantity_1,
(select s.qty
from stores as s
where s.productid = p.productid and s.warehouse = 2
) as quantity_2
from products as p;
注意:这假设stores
表中每个仓库/产品组合都有一行。如果有多个行,则需要某种形式的聚合,例如sum(s.qty)
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句