MySQL删除30天以前的数据(PHP)

需求:

1.每日定时执行清理30天以前旧数据,如:当前日期2019年5月8日,需清理所有小于2019年4月8日数据

2.直接通过sql语句实现

/*
 * 定时任务,每日0:00执行,删除一个月以前的短链接
 * 0 0 * * * /usr/local/php/bin/php 01-delete_surl.php
 */
$link = new mysqli('127.0.0.1', 'root', 'root', 'surl', 3306);
$sql = "delete from information where information like '%articledetails%' and time < date_sub(curdate(), INTERVAL 30 DAY)";
// 或者
// $sql = "delete from information where information like '%articledetails%' and date(time) < date_sub(curdate(), INTERVAL 30 DAY)";
mysqli_query($link, $sql) or die('删除数据出错:' . mysqli_error($link));
$link->close();

猜你喜欢

转载自blog.csdn.net/tt745/article/details/113913360
今日推荐