0

我的 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_QuantitybillsItems_ItemDiscount billsItems_ItemTotal在同一个查询中,我需要获取表的所有项目。

错误 :

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in
4

3 回答 3

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 
于 2012-12-18T16:02:56.987 回答
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 
于 2012-12-18T16:00:53.033 回答
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
于 2012-12-18T16:03:48.953 回答