mysql - 查询一个人一年内购买了多少次

标签 mysql sql heidisql

大家好,我有一张 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/

相关文章:

mysql - 查询更新 HeidiSql 中表中的外键行

mysql - 如何为订单及其项目编写自定义 MySQL 选择查询

sql - 如何声明一个变量以从不同的表列中读取?

SQL Server Express 'sa' 登录失败

java - Java Spring Boot 项目中的存储过程返回 null 作为输出

mysql - 使用一个表中的同一列和另一表上的两个外键

mysql - 带有 WHERE 子句的更新语句

mysql - hibernate在提交之前刷新之后获取数据

php - 使用php从mysql数据库中获取信息

mysql - 如何在一张表中创建多个实例?无法写入空行,因为它在 Mysql 中为 "Read Only"