简体   繁体   中英

PHP/MySql compare data between last 7 days and before last 7 days

I am trying to compare the count of my enquiries for last 7 days data (24-10-2018 to 31-10-2018) with before that weeks data (16-10-2018 to 23-10-2018). My enquiry Mysql table structure is as follows:

=================================
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 

Just to start can anyone give me a guideline on how to start with it. Can i achieve this by one query?

Hi Sanju this is not tested but its help you, First check your date format than try below

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM