2013-02-04 68 views
3

我想在這裏提出一個問題......讓我解釋一下... 這裏我有3個表, 1. transaction_table 2. transaction_detail 3項mysql的項目銷售

不,我這裏的問題是我想知道的項目本月銷售(2013-01)中的示例中,這裏是我的表結構...

1交易表

-------------------------- 
idTransaction | date  | idUser | idCustomer | total | NOSC 
-------------------------------------------------------------------- 
1    | 2013-01-01 | 3  | 4   | 500000 | 1CO 
2    | 2013-01-01 | 3  | 5   | 450000 | 2CO 
3    | 2013-01-01 | 3  | 6   | 250000 | 3CO 

2 transaction_detail

----------------------------------------------------------------- 
idTransaction | idItem | qty | price 
---------------------------------------- 
1    | 1  | 2 | 250000 
2    | 2  | 1 | 250000 
2    | 3  | 1 | 200000 
3    | 1  | 1 | 250000 

3項表

idItem | Name Item 
--------------------------------------- 
1  | glass 
2  | Medicine A 
3  | Medicine B 

4客戶

idCustomer | Name Customer 
    -------------------------------------- 
    4    | Abuh 
    5    | Abeh 
    6    | ABooh 

因此,基於這些表我想獲得這樣的數據....

Name Item | Nosc | Customer | Sold Quantity | @ Price | Total 
--------------------------------------------- 
Glass  | 1CO | Abuh  |2    | 250000 | 500000 
Glass  | 3CO | ABooh |1    | 250000 | 250000 
Medicine A| 2CO | Abeh  |1    | 250000 | 250000 
Medicine B| 2CO | Abeh  |1    | 200000 | 200000 

任何人都可以幫助我?

回答

4
SELECT b.`name Item`, 
     a.qty `Sold Quantity`, 
     a.price `@ price`, 
     (a.qty * a.price) Total 
FROM transaction_detail a  
     INNER JOIN Item b 
      ON a.idItem = b.idItem 

上述查詢基於所述記錄並在該示例的結果給出。 後續問題:有沒有可能以某種不同的價格銷售某種商品?如果是這樣,你如何計算它?

UPDATE 1

SELECT b.`name Item`, 
     SUM(a.qty) `Sold Quantity`, 
     a.price `@ price`, 
     (SUM(a.qty) * a.price) Total, 
     c.Date 
FROM transaction_detail a  
     INNER JOIN Item b 
      ON a.idItem = b.idItem 
     INNER JOIN `transaction` c 
      ON a.idtransaction = c.idTransaction 
GROUP BY b.idItem, b.`name Item`, a.price, c.Date 

UPDATE 2

SELECT b.`name Item`, 
     SUM(a.qty) `Sold Quantity`, 
     a.price `@ price`, 
     (SUM(a.qty) * a.price) Total, 
     d.`Name Customer` 
FROM transaction_detail a  
     INNER JOIN Item b 
      ON a.idItem = b.idItem 
     INNER JOIN `transaction` c 
      ON a.idtransaction = c.idTransaction 
     INNER JOIN Customer d 
      ON d.idCustomer = c.idCustomer 
GROUP BY b.idItem, b.`name Item`, a.price, 
      MONTH(c.Date), YEAR(c.Date), d.`Name Customer` 
+0

我需要使用從事務表的日期..查詢的方式是什麼? –

+0

@WawanBrutalx你需要加入'Transaction'表。 [** >> CLICK_HERE << **](http://www.sqlfiddle.com/#!2/f7090/5) –

+0

我的後續問題如何? –

1
select it.item_name,td.qty,td.price,t.total 
    from transaction as t,transaction_detail as td,item_table as it 
    where t.idTransaction=td.idTransaction 
    and td.idItem=it.idItem 
    and t.date between 'YEAR(CURDATE())."-".MONTH(CURDATE())."-"."01"' 
        and 'YEAR(CURDATE())."-".MONTH(CURDATE())."-"."31"'