[英]Error on query by sum and fetch row - sql
i have error on my sql query. 我的SQL查询有错误。
my query : 我的查询:
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
In my query I need to get sum of billsItems_Quantity
, billsItems_ItemDiscount
billsItems_ItemTotal
by use sum function , in the same query I need to get all item of the table. 在我的查询中,我需要使用sum函数获取billsItems_Quantity
和billsItems_ItemDiscount
billsItems_ItemTotal
的总和,在同一查询中,我需要获取表的所有项目。
the error : 错误 :
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
Ok I think I understand your problem. 好的,我想我理解您的问题。 Are you usign SQL Server 2005 or newer? 您使用的是SQL Server 2005或更高版本吗?
If so, you can do this with the "OVER" clause, wich will let you do sums and get all the other columns. 如果是这样,您可以使用“ OVER”子句来执行此操作,其中wich将允许您进行求和并获得所有其他列。 here's a way to do this 这是一种方法
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
I guess it is because you did not add GROUP BY, try 我想这是因为您没有添加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
You have not provided a lot of details, but you might be able to use a subquery for the sum()
and then join back to your table: 您没有提供很多详细信息,但是您可以对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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.