我想使用2个表格显示每张帐单的销售商品总价值 -
这是表项,表的结构是I_Code-> int,Name-> Varchar,Category-> Varchar,Rate-> int
这是表格账单,表格的结构是BillNo-> int,Date-> date, I_Code-> int,qty-> int
以下是我想要获得所需输出的查询
SELECT i.`rate` * b.`qty`, b.`BillNo` FROM bills b, items i WHERE b.I_Code=i.I_Code GROUP BY `BillNo` ;
答案 0 :(得分:1)
试试这个:
SELECT SUM(A.TOT), b.`BillNo`
FROM (
SELECT i.`rate` * b.`qty` AS TOT, b.`BillNo`
FROM bills b, items i
WHERE b.I_Code = i.I_Code
) AS A
GROUP BY `BillNo`;
或
SELECT SUM(i.`rate` * b.`qty`) AS TOT, b.`BillNo`
FROM bills b, items i
WHERE b.I_Code = i.I_Code
GROUP BY `BillNo`;