SQLに問題があります。
今、私はアイテムテーブルを持っていて、私が販売するアイテムの数を計算するつもりはありません、私がこのように一度に1つのアイテムを販売するならば、私はそれをすることができます:
SELECT count(Quantity) AS ItemQ from item group by item_name
しかし、私が同じアイテムを購入する場合、私たちはここで販売を購入してサブカウントし、0に等しくする必要があります
したがって、結果には2つのアイテムが表示される必要がありますが、group byを使用しており、同じアイテムの数を計算できません。
私の完全なクエリ:
SELECT count(billsItems_Quantity) AS ItemQ , billsItems_ItemDiscount , billsItems_ItemName , billsItems_Price , billsItems_SecondPrice , billsItems_ItemTotal ,bills_StoreID , bills_UserID , bills_ID , billsItems_BillItemSerial, cashier_trans_DocId , billsitems_ItemID , billsItems_Barcode
FROM bills , billsitems , cashier_trans
WHERE
bills.bills_ID = billsitems.billsItems_BillItemSerial
and bills.bills_ID = cashier_trans.cashier_trans_DocId
and bills.bills_StoreID = '$ausers_StoreId'
and bills.bills_UserID = '$ausers_ID'
and bills.bills_Status = '1'
アイテムの表は
id item_name bill_type count_item price_item
1 apple sale 1 20
2 apple buy 1 15
3 tea sale 1 10
4 apple sale 1 20
今、リンゴの数は私に1no3を示さなければなりません
SQLでこれを行うにはどうすればよいですか。