简体   繁体   中英

MySQL query to fetch result next 7 days when comparing 2 columns timestamps

I have 2 columns in same table, I am trying to count all id's which expiry date is greater +7 days from today column time-stamp

Please take a look into my table

user_pages

id |        expiry         |       today         |   Flag |
1  | 2016-02-08 15:03:57   | 2016-03-24 07:17:01 |    0   |
2  | 2016-03-31 07:17:01   | 2016-03-24 07:17:01 |    1   |

I trying this:

("SELECT COUNT(id) AS aggeorders FROM user_pages 
   WHERE user_id ='$id' 
   And expiry > today + INTERVAL 7 DAY)");

also I need to run a cronjob to set flag = 1

("update user_pages 
   if expiry > today + INTERVAL 7 DAY) set flag = 1");

However neither of them worked. Any ideas how to accomplish it?

Try this:

Query #1:

SELECT COUNT(id) AS aggeorders FROM user_pages 
WHERE user_id ='$id' 
And expiry >= today + INTERVAL 7 DAY

Query #2:

update user_pages 
set flag = 1
where expiry >= today + 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