开发者

Help with MySQL date operation

开发者 https://www.devze.com 2022-12-31 12:54 出处:网络
Hello i have two columns: duration (int) an开发者_StackOverflow社区d dateCol (date) in a table. How should i proceed if i want to know when CURDATE() - dateCol will iqual to duration.

Hello i have two columns: duration (int) an开发者_StackOverflow社区d dateCol (date) in a table. How should i proceed if i want to know when CURDATE() - dateCol will iqual to duration.

ie: CURDATE() - dateCol (2010-05-08) = duration (14 days), and then send an e-amil?


DATE_SUB(CURDATE(), INTERVAL 14 DAY) = datecol


To do what you want you can use DATEDIFF:

DATEDIFF(NOW(), last_email_date) >= 14

An alternative method is to interval subtraction:

CURDATE() - interval 14 day >= last_email_date

The second can be faster if there is a suitable index.

Note that I've chosen to use an inequality test instead of an equality because this will handle missed emails if your process is offline for one day and is restarted the next day. You should mark the row as handled when you've handled it to prevent more than one email being sent. This will also prevent multiple emails being sent if your process runs more than once per day (this could happen if you restart it to upgrade / bug fix).

0

精彩评论

暂无评论...
验证码 换一张
取 消