我有一个主表和一个子表。主表是零件的主列表,子表是构建零件所需的集合零件。每个子部分都通过ParentID链接到其父部分。
零件或“子零件”有一个ChildID,它指向主要零件清单中它自己的条目。我们使用此链接来获取价格,因为在子表中仅存储ID。
如果我想将构成每个父部件的所有子部件的总成本相加。我将如何构造该查询?
总成本应该是一个计算字段,该字段将选择子表中所有具有ID的ParentID ==的部件,并将其总价*数量相加。
这是我到目前为止所拥有的:
//This Query Doesnt work. Just returns 0.
SELECT SUM(PricePer * Qty)
FROM sub inner join main on sub.ChildPID = main.ID where sub.ParentPID = main.ID
//This query will work, where the main.ID is entered manually. but I cant do this
SELECT SUM(PricePer * Qty)
FROM sub inner join main on sub.ChildPID = main.ID where sub.ParentPID = 1
示例:
主表
ID | Name | TotalCost | PricePer
1 Part1 10.00 14.00** SELECTED
2 Part2 0.00 6.00
3 Part3 0.00 2.00
子表
ParentID | ChildID | Name | Qty
1 2 Part2 1
1 3 Part3 2
答案 0 :(得分:1)
您不应该在左连接中使用where子句,否则会作为内部连接
并且如果您想将结果与上层查询联系起来..您对总和进行分组,并按与上层查询相关的关键字进行分组
您可以直接根据合并的结果执行更新,例如:
update main
inner join (
SELECT main.ID, SUM(main.PricePer) total_x_id
FROM sub
INNER join main on sub.ChildID = main.ID
group by main.ID
) t on t.id = main.id
set total = t.total_x_id
如果您只需要选择,则
SELECT main.ID, SUM(main.PricePer) total_x_id
FROM sub
INNER join main on sub.ChildID = main.ID
group by main.ID
您可以使用
作为连接在上层查询中获得结果select maint.id, ifnull(t.total_x_id ,0)
from main
left join (
SELECT ChildID.ID as ID, SUM(main.PricePer) total_x_id
FROM sub
group by ChildID.ID
) t on t.ID = main.id
答案 1 :(得分:1)
这是一个级别的选择:
SELECT m.priceper + SUM(s1.qty * m2.priceper) AS priceper
FROM main m
LEFT JOIN sub s1 ON (m.id = s1.parentid)
LEFT JOIN main m2 ON (m2.id = s1.childid)
-- WHERE m.id = 1
GROUP BY m.id;
对于2个级别,将是这样的(我无法尝试,可能需要一些修复):
SELECT m1.id, m1.priceper + IFNULL(SUM(s1.qty * g2.priceper),0) AS priceper
FROM main m1
LEFT JOIN sub s1 ON (m1.id = s1.parentid)
LEFT JOIN (
SELECT m2.id, m2.priceper + IFNULL(SUM(s2.qty * g2.priceper),0) AS priceper
FROM main m2
LEFT JOIN sub s2 ON (m2.id = s2.parentid)
LEFT JOIN main g2 ON (g2.id = s2.childid)
GROUP BY m2.id)
g2 ON (g2.id = s1.childid)
-- WHERE m1.id = 1
GROUP BY m1.id;
要向下钻取,您需要用g3等的子查询替换主表...
SELECT m1.id, m1.priceper + IFNULL(SUM(s1.qty * g2.priceper),0) AS priceper
FROM main m1
LEFT JOIN sub s1 ON (m1.id = s1.parentid)
LEFT JOIN (
SELECT m2.id, m2.priceper + IFNULL(SUM(s2.qty * g2.priceper),0) AS priceper
FROM main m2
LEFT JOIN sub s2 ON (m2.id = s2.parentid)
LEFT JOIN (
SELECT m3.id, m3.priceper + IFNULL(SUM(s3.qty * g3.priceper),0) AS priceper
FROM main m3
LEFT JOIN sub s3 ON (m3.id = s3.parentid)
LEFT JOIN main g3 ON (g3.id = s3.childid)
GROUP BY m3.id) g2
ON (g2.id = s2.childid)
GROUP BY m2.id) g2
ON (g2.id = s1.childid)
-- WHERE m1.id = 1
GROUP BY m1.id;
对于4个级别,它看起来像这样:
SELECT m1.id, m1.priceper + IFNULL(SUM(s1.qty * g2.priceper),0) AS priceper
FROM main m1
LEFT JOIN sub s1 ON (m1.id = s1.parentid)
LEFT JOIN (
SELECT m2.id, m2.priceper + IFNULL(SUM(s2.qty * g2.priceper),0) AS priceper
FROM main m2
LEFT JOIN sub s2 ON (m2.id = s2.parentid)
LEFT JOIN (
SELECT m3.id, m3.priceper + IFNULL(SUM(s3.qty * g3.priceper),0) AS priceper
FROM main m3
LEFT JOIN sub s3 ON (m3.id = s3.parentid)
LEFT JOIN (
SELECT m4.id, m4.priceper + IFNULL(SUM(s4.qty * g4.priceper),0) AS priceper
FROM main m4
LEFT JOIN sub s4 ON (m4.id = s4.parentid)
LEFT JOIN main g4 ON (g4.id = s4.childid)
GROUP BY m4.id) g3
ON (g3.id = s3.childid)
GROUP BY m3.id) g2
ON (g2.id = s2.childid)
GROUP BY m2.id) g2
ON (g2.id = s1.childid)
-- WHERE m1.id = 1
GROUP BY m1.id;
答案 2 :(得分:0)
这变得比我想象的要复杂。也许这是我的错,因为我不清楚。这是我发现可行的解决方案。
Select *, ifnull((SELECT SUM(main.PricePer * Qty)
FROM sub inner join main on sub.ChildID = main.ID where sub.ParentID = main1.PID), 0) from main as main1