大家好,我有一张 table (商店),有 3 列:
id receipt_number receipt_date
111 666 02/02/2016
111 666 02/02/2016
111 667 02/03/2016
112 668 02/02/2016
112 669 03/02/2015
112 670 04/12/2015
113 671 05/11/2013
我想查询每个ID在去年购买了多少次。 决赛 table :
id bought
111 2
112 2
111 have 2 shop because in database have 2 equals receipt
112 have 2 shop because in database one shop It is a year old
113 there is not because the only shop it has been done years ago.
现在我已经这样做了:
SELECT DISTINCT(id),receipt_number,receipt_date
FROM shop
where receipt_number in (SELECT distinct(receipt_number)
FROM shop)
GROUP BY(id)
但完全错误
谢谢大家,很抱歉我的英语不好
我已经找到结果了,谢谢大家
select id, count(*)shop_number
from
(select id,recipt_Date,recipt_number
from shop
where id !='' and recipt_date > DATE_ADD(sysdate(), INTERVAL -1 YEAR)
and recipt_Date <= sysdate()
group by id, recipt_date , recipt_nu,ber) as a
group by id
最佳答案
我认为你需要这样的东西:
SELECT id, COUNT(DISTINCT receipt_number)
FROM shop
WHERE receipt_date BETWEEN DATE_ADD(NOW(), INTERVAL -1 YEAR) AND NOW()
GROUP BY id
关于mysql - 查询一个人一年内购买了多少次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35797717/