开发者

SQL DATETIME query

开发者 https://www.devze.com 2023-03-13 11:09 出处:网络
Hi im trying to compare two dates in MySQL via a query, one is a date when an article was first posted and the second date is for when it should be removed by the query (after 365 days, ran by a cron

Hi im trying to compare two dates in MySQL via a query, one is a date when an article was first posted and the second date is for when it should be removed by the query (after 365 days, ran by a cron task every 30 minutes.), below is my SQL query

DELETE FROM $wpdb->posts WHERE post_type = 'business' AND DATEDIFF(NOW(), post_date_gmt) > 2 

My date is held in the开发者_如何学JAVA database in this format 2011-05-26 13:10:56 so my question is will the DATETIME query honour the exact date and time or when cron is ran at the 365th day delete the article even if there are still 13:10:56 left before it is actually 365 days old?

Regards


Your query, as written, will delete any posts older than 2 days--not 365.

But the answer to your question is easy to test:

mysql> SELECT DATEDIFF('2011-01-01 00:10:00','2011-01-01 00:00:00');
+-------------------------------------------------------+
| DATEDIFF('2011-01-01 00:10:00','2011-01-01 00:00:00') |
+-------------------------------------------------------+
|                                                     0 | 
+-------------------------------------------------------+
1 row in set (0.00 sec)

As you can see, DATEDIFF() returns only whole days, as an integer. A similar test with '2011-01-01 23:59:59' as the first date will also yield 0.

So the short answer to your question is "Yes, it honors the date and time, down to the second."

0

精彩评论

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

关注公众号