多表内连接和聚合函数的使用问题(group by having子句)
简要的说
我有三个表
图书信息表: tb_book
字段: 条形码 书名
销售主表 tb_sell_main
字段: 销售单号 时间
销售明细表 tb_sell_sub
字段: 销售单号 条形码 数量
现在我要知道在某段时间内 图书的销量排行(图书名 销量)
我的查询语句是:
select a.书名, SUM(c.数量) from (tb_book a inner join tb_sell_sub c on a.条形码 = c.条形码) \
inner join tb_sell_main b on c.销售单号 = b.销售单号 \
group by a.条形码, a.书名 \
having b.时间 between '2013/2/6' and '2013/5/21' order by SUM(c.数量) desc
SELECT a.书名 ,
SUM(c.数量)
FROM ( tb_book a
INNER JOIN tb_sell_sub c ON a.条形码 = c.条形码
)
INNER JOIN tb_sell_main b ON c.销售单号 = b.销售单号
WHERE b.时间 BETWEEN '2013/2/6' AND '2013/5/21'
GROUP BY a.条形码 ,
a.书名
ORDER BY SUM(c.数量) DESC