我有一个sql请求,它在phpmyadmin中工作但是当我尝试制作Viev时出现错误:
如何避免在FROM子句中使用子查询?这是sql:
Select tlb_all.BarCode As BarCode,
Sum(tlb_all.TotalIncomes) As TotalIncomes,
Sum(tlb_all.TotalOutcomes) As TotalOutcomes,
Sum(tlb_all.TotalAdjustments) As TotalAdjustments,
Sum(tlb_all.TotalIncomes - tlb_all.TotalOutcomes + tlb_all.TotalAdjustments)
As Leftover
From (Select tlb1.BarCode As BarCode, Sum(IfNull(tlb1.Quantity,
0)) As TotalIncomes, Sum(0) As TotalOutcomes, Sum(0) As TotalAdjustments
From stock_incomes tlb1
Group By tlb1.BarCode
UNION
Select tlb2.BarCode As BarCode, Sum(0) As TotalIncomes,
Sum(IfNull(tlb2.Quantity, 0)) As TotalOutcomes, Sum(0) As
TotalAdjustments
From stock_outcomes tlb2
Group By tlb2.BarCode
UNION
Select tlb3.BarCode As BarCode, Sum(0) As TotalIncomes,
Sum(0) As TotalOutcomes, Sum(IfNull(tlb3.Adjustment,
0)) As TotalAdjustments
From stock_adjustments tlb3
Group By tlb3.BarCode
UNION
Select tlb4.BarCode As BarCode, Sum(0) As TotalIncomes,
Sum(0) As TotalOutcomes, Sum(0) As TotalAdjustments
From stock_outcomes tlb4
Group By tlb4.BarCode) As tlb_all
Group By tlb_all.BarCode
Having IsNull(tlb_all.BarCode) = FALSE And Leftover <> 0
Order By tlb_all.BarCode
答案 0 :(得分:0)
如果你有一张包含所有可能代码的表,你可以这样做::
select bc.BarCode,
(Select Sum(si.Quantity) From stock_incomes si where si.BarCode = bc.BarCode
) as TotalIncomes,
. . .
from BarCodes bc
也就是说,将子查询移动到select
子句(允许这些子句)。