PHP / MySql比较过去7天和最近7天之间的数据

时间:2018-10-31 10:04:49

标签: php mysql

我正在尝试比较最近7天(24-10-2018至31-10-2018)的查询计数与该周之前(16-10-2018至23-10-2018)的查询计数。我的查询Mysql表结构如下:

=================================
id | enquiry_date | enquiry_desc
=================================
 1     16-10-2018    Test Data
 2     17-10-2018    Test Data
 3     18-10-2018    Test Data
 4     18-10-2018    Test Data
 5     20-10-2018    Test Data
 6     24-10-2018    Test Data
 7     25-10-2018    Test Data
 8     28-10-2018    Test Data
=================================

So the result should be 

Last week count (16-10-2018 to 23-10-2018) : 5
This Week count (24-10-2018 to 31-10-2018) : 3 

任何人都可以给我关于如何开始使用的指南。我可以通过一个查询来实现吗?

1 个答案:

答案 0 :(得分:1)

您好,Sanju尚未经过测试,但可以帮助您,首先请检查您的日期格式,然后尝试用下面的方法

SELECT count(id) AS last_week FROM table_name
WHERE enquiry_date >= date_sub(current_date, INTERVAL 14 day) 
AND enquiry_date < date_sub(current_date, INTERVAL 7 day)
UNION
SELECT count(id) AS this_week FROM table_name
WHERE enquiry_date >= date_sub(current_date, INTERVAL 7 day)