在DELETE语句中指定“old than”

时间:2013-01-19 16:25:24

标签: php mysql sql

我有一个查询,我想用它来删除“已查看”列日期时间值超过24小时的数据行。如何使用一个查询执行此操作?可能吗?

这是我到目前为止所做的:

$delete_expired_notifications_query= "DELETE FROM notifications WHERE user_id= 
'".$u_id."' AND viewed (some code that says older than 24hrs)";
    mysqli_query($connect, $delete_expired_notifications_query);

3 个答案:

答案 0 :(得分:3)

假设viewed是MySQL时态类型(它应该):

... AND viewed < NOW() - INTERVAL 24 HOUR

答案 1 :(得分:2)

使用date_Add和间隔,您可以尝试从现在开始超过24小时的记录..

DELETE FROM notifications WHERE user_id= 
'".$u_id."' AND viewed < date_Add(now(), interval - 24 hour)

答案 2 :(得分:1)

DELETE FROM notifications WHERE user_id = '".$u_id."' 
      AND viewed < DATE_ADD(NOW(), INTERVAL -1 DAY)";

DELETE FROM notifications WHERE user_id = '".$u_id."' 
      AND viewed < DATE_ADD(NOW(), INTERVAL -24 HOUR)";