我想删除表格中的所有数据,并使用比例如2天更长的邮票。我创建了以下查询:
DELETE FROM temporary_data WHERE stamp < CURDATE() - INTERVAL 1 DAY;
但这似乎不起作用。我的邮票看起来像这样:2014-04-29 15:37:00
我认为CURDATE()只返回2014-04-30,这与邮票不匹配。谁能帮帮我?
答案 0 :(得分:1)
从现在开始的2天以上可以删除
DELETE FROM temporary_data WHERE stamp < date_sub(now(),INTERVAL 2 day)
这里如何在mysql中看起来像
mysql> select now(),date_sub(now(),INTERVAL 2 day) ;
+---------------------+--------------------------------+
| now() | date_sub(now(),INTERVAL 2 day) |
+---------------------+--------------------------------+
| 2014-04-30 12:54:39 | 2014-04-28 12:54:39 |
+---------------------+--------------------------------+
没有H:i:s
,它应该是
mysql> select CURDATE(),date(date_sub(now(),INTERVAL 2 day)) ;
+------------+--------------------------------------+
| CURDATE() | date(date_sub(now(),INTERVAL 2 day)) |
+------------+--------------------------------------+
| 2014-04-30 | 2014-04-28 |
+------------+--------------------------------------+
答案 1 :(得分:1)
始终在删除前选择
SELECT from temporary_data
where stamp between date_sub(now(),INTERVAL 2 DAY) and now();
删除查询
delete from temporary_data
where stamp between date_sub(now(),INTERVAL 2 DAY) and now();
您可以根据您的要求修改时间间隔,以获得更多阅读http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html