简体   繁体   English

从时间戳超过一天的MySQL表中删除行?

[英]Removing rows from MySQL table where the timestamp is over one day old?

I found the exact same question here . 我在这里找到了完全相同的问题。

But it isn't working for me. 但这对我不起作用。 I've modified it a bit, manipulated it, and I can't figure it out. 我修改了一下,操纵它,我无法弄明白。 I'm trying to remove rows that are over a day old. 我正在尝试删除超过一天的行。 Here is my code: 这是我的代码:

if (isset($_POST['prune'])) {

    $sql = "DELETE FROM logs WHERE time < date('now', '-1 days')";
    mysql_query($sql);

    echo 'Logs older than one day removed.';    

    }

Fairly simple question I suppose, but its bugging the hell out of me. 我想这是一个相当简单的问题,但它却让我不知所措。 I would appreciate any help. 我将不胜感激任何帮助。

In case it makes a difference, the column is a TIMESTAMP type. 如果它有所不同,该列是TIMESTAMP类型。

EDIT: Apparently I'm an idiot. 编辑:显然我是个白痴。 The question I linked you to relates to SQLite3. 我链接到你的问题与SQLite3有关。 So now my question is, how can I do this in MySQL? 所以现在我的问题是,我怎样才能在MySQL中做到这一点?

您可以减去一个间隔:

DELETE FROM logs WHERE time < now() - interval 1 day

That answer was IIRC for SQLite3. 答案是SQLite3的IIRC。 You're using MySQL which does not support this syntax. 您正在使用不支持此语法的MySQL。

You want to use DATE_ADD() function (example below not tested but should work): 您想使用DATE_ADD()函数(以下示例未经测试但应该有效):

DELETE FROM logs WHERE time < TIMESTAMPADD(DAY,-1,NOW());

In my case, only the 就我而言,只有

timestampadd

in positive direction works when running through an asc ordered date calendar: 在asc订购日期日历中运行时,正方向工作:

    if (urldecode ($aUrl['Select']) == '>')
        $this-> db-> where ('konzertdatum >', 'TIMESTAMPADD(DAY, 1, "'.  $id . '") ', false) ;
    else 
        $this-> db-> where ('konzertdatum < ', 'TIMESTAMPADD(DAY, -1, "'.  $id . '") ', false) ;

In negative direction ( -1) , the timestamp goes back to the very first entry. 在负方向( -1) ,时间戳返回到第一个条目。 I also tried not to use CodeIgniter, but the same consequence. 我也尝试过不使用CodeIgniter,但结果相同。

Summing up, +1 as parameter for timestampadd works, -1 works for the second lowest entry (then jumping back to the first one. But it fails when I step further away from the first date. 总结, +1作为timestampadd的参数工作, -1适用于第二个最低的条目(然后跳回到第一个。但是当我离第一个日期更远时它失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM