MySQL DATETIME DIFF query

此生再无相见时 提交于 2019-12-19 05:34:04

问题


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


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/6410642/mysql-datetime-diff-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!