获取按总销售额分组的产品?

时间:2010-06-14 18:26:41

标签: php mysql aggregate-functions

我有以下MySQL表:

TABLE: Products
----------------------
 id   |   productname
 1030 |   xBox 360
 1031 |   PlayStation 3
 1032 |   iPod Touche

TABLE: Sales
----------------------
 productid | saledate
 1031      | 2010-06-14 06:30:12
 1031      | 2010-06-14 08:54:38
 1030      | 2010-06-14 08:58:10
 1032      | 2010-06-14 10:12:47

我想使用php获取我今天销售的产品,并按销售数量和销售日期(如果可能)订购产品,输出示例:

 Today's statistics:
 -Playstation 3 (2 sales)
 -Xbox 360 (1 sale)
 -iPod Touche (1 sale)

由于

1 个答案:

答案 0 :(得分:0)

大卫,

(未经测试)的内容:

select 
p.productname,
count(s.productid)
from sales s inner join products p
on p.id=s.productid
where s.saledate=curdate()
group by p.productname

正如我所说的那样,未经测试,但“感觉”就像它会做你所追求的......

吉姆