mysql项目销售

时间:2013-02-04 07:25:06

标签: php mysql sql join

我想在这里问一个问题......让我来解释一下...... 这里我有3张桌子, 1. transaction_table 2. transaction_detail 3.项目

不,我的问题是我想知道本月(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

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分: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

上面的查询基于示例中给出的记录和结果。 后续问题:是否有可能以不同价格出售某件商品?如果是这样,你如何计算它?

更新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

更新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`

答案 1 :(得分: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"'