我的SQL查询出错了。
我的疑问:
SELECT sum(billsItems_Quantity) as TotalQ ,
sum(billsItems_ItemDiscount) as TotalD ,
sum(billsItems_ItemTotal) as TotalI ,
bills.bills_ID ,
billsitems.billsItems_BillItemSerial ,
bills.bills_CashierID ,
bills.bills_StoreID ,
billsitems.billsItems_Unit ,
billsitems.billsItems_Price
FROM bills , billsitems
WHERE bills.bills_ID = billsitems.billsItems_BillItemSerial
and bills.bills_CashierID = '".$id."'
and bills.bills_StoreID = '$ausers_StoreId'
order by billsitems.billsItems_Unit
在我的查询中,我需要通过使用sum函数得到billsItems_Quantity
,billsItems_ItemDiscount
billsItems_ItemTotal
的总和,在我需要的相同查询中获取表的所有项目。
错误:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
答案 0 :(得分:0)
好的,我想我理解你的问题。您是使用SQL Server 2005还是更新版?
如果是这样,您可以使用“OVER”子句执行此操作,这将使您可以执行求和并获取所有其他列。这是一种方法来做到这一点
SELECT sum(billsItems_Quantity) OVER (PARTITION BY /*group by columns for the sum here*/) as TotalQ ,
sum (billsItems_ItemDiscount) OVER (PARTITION BY /*group by columns for the sum here*/) as TotalD ,
sum (billsItems_ItemTotal) OVER (PARTITION BY /*group by columns for the sum here*/) as TotalI ,
bills.bills_ID , billsitems.billsItems_BillItemSerial , bills.bills_CashierID ,
bills.bills_StoreID , billsitems.billsItems_Unit , billsitems.billsItems_Price FROM bills ,
billsitems
WHERE bills.bills_ID = billsitems.billsItems_BillItemSerial
and bills.bills_CashierID = '".$id."'
and bills.bills_StoreID = '$ausers_StoreId'
order by billsitems.billsItems_Unit
答案 1 :(得分:0)
我想这是因为您没有添加GROUP BY,请尝试
SELECT sum(billsItems_Quantity) as TotalQ ,
sum(billsItems_ItemDiscount) as TotalD ,
sum(billsItems_ItemTotal) as TotalI ,
bills.bills_ID ,
billsitems.billsItems_BillItemSerial ,
bills.bills_CashierID ,
bills.bills_StoreID ,
billsitems.billsItems_Unit ,
billsitems.billsItems_Price
FROM bills , billsitems
WHERE bills.bills_ID = billsitems.billsItems_BillItemSerial
and bills.bills_CashierID = '".$id."'
and bills.bills_StoreID = '$ausers_StoreId'
GROUP BY bills.bills_ID ,
billsitems.billsItems_BillItemSerial ,
bills.bills_CashierID ,
bills.bills_StoreID ,
billsitems.billsItems_Unit ,
billsitems.billsItems_Price
ORDER BY billsitems.billsItems_Unit
答案 2 :(得分:0)
您没有提供很多详细信息,但您可以使用sum()
的子查询,然后加入到您的表中:
SELECT i1.TotalQ ,
i1.TotalD ,
i1.TotalI ,
b.bills_ID ,
i2.billsItems_BillItemSerial ,
b.bills_CashierID ,
b.bills_StoreID ,
i2.billsItems_Unit ,
i2.billsItems_Price
FROM bills b
INNER JOIN
(
select sum(billsItems_Quantity) TotalQ,
sum(billsItems_ItemDiscount) as TotalD,
sum(billsItems_ItemTotal) as TotalI,
billsItems_BillItemSerial
from billsitems
group by billsItems_BillItemSerial
) i1
ON b.bills_ID = i1.billsItems_BillItemSerial
inner join billsitems i2
on b.bills_ID = i2.billsItems_BillItemSerial
WHERE b.bills_CashierID = '".$id."'
and b.bills_StoreID = '$ausers_StoreId'
order by i2.billsItems_Unit