开发者

MySQL DATETIME DIFF query

I have a MySQL query that runs via cron every 30 minutes to delete old property listings, the query is:

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

right now its in a testing phase and set to delete when the listing is 2 days old, this it does without a problem, the problem that i do have is that i need it to recognise the time from when the listing was posted and the time to when it should be deleted,

Basically the table column of post_date_gmt is in the format of 2011-05-26 13:10:56 and the column type is DATETIME when i have the DATEDIFF (NOW() query running it needs to be equal to 48 hours or m开发者_运维百科ore from current time to delete the listing, this is not happening, the query just seems to say "this is the 2nd day, delete listing" so it gets deleted and this could be when just 24 and a half hours old, how can i make it go the full exact 48 hours?

Regards


Try timediff with a 48 hours delta instead . Or use a 3 days delta if precision is not an issue.

Try HOUR(TIMEDIFF(NOW(), date))

SELECT count(*) FROM $wpdb->posts WHERE post_type = 'rentals' AND HOUR(TIMEDIFF(NOW(),  post_date_gmt)) >=48

There is a lot of MySQL date/time function : http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_hour

Note: use SELECt before trying DELETE


you can DATEDIFF(NOW(), post_date_gmt) >= 3 and it will be deleted 48 and half hours old

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜