日期:2014-05-17  浏览次数:20626 次

如何查出三天内消费总值为500以内的数据
我用到两个表,一个是customer,一个是order
customer表的结构和数据如下
customerId    name
1              李三
2              王五
3              刘六
4              黄七

order表的结构和数据如下
orderId   customerId    amount    orderTime
1          1             53        2013-05-31
2          2             100       2013-06-01
3          1             70        2013-06-01
4          2             500       2013-06-02
5          1             200       2013-06-02
6          3             400       2013-06-02
我想查出最近三天()今天,昨天,前天)的消费总额小于500的用户,这个例子里的结果就是

customerId    name    total
1              李三    323
3              刘六    400
这个查询句怎么写,多谢!

------解决方案--------------------

select a.customerid,a.name,sum(b.amount) total
from customer a,[order] b
where a.customerid=b.customerid 
and b.ordertime between '2013-05-31' and '2013-06-02' group by a.customerid,a.name
having sum(b.amount)<500