简体   繁体   English

从表中选择所有早于3天的条目

[英]Select all entries from a table which are older then 3 days

I've this table in my database: 我的数据库中有此表:

-------------------------------------------------
| id | name | model | inserted_time (timestamp) |
-------------------------------------------------

When I insert something into my table the inserted_time get's set automatically so this is the current timestamp. 当我在表中插入内容时, insert_time将自动设置,因此这是当前时间戳。


Now I need to do a select on my table to get all entries wich are 3 days older then the inserted_time timestamp. 现在,我需要在表上进行选择,以使所有条目都比插入时间时间戳早3天。 I'm doing this in PHP with wpdb in WordPress. 我正在用WordPress中的wpdb在PHP中执行此操作。 This is what I already have: 这就是我已经拥有的:

$results = $wpdb->get_results( 'SELECT * FROM  my_table WHERE inserted_time /*OLDER THEN 3 DAYS */' );

For example when I've following entry in my table: 例如,当我关注表中的条目时:

-------------------------------------------------
| id | name | model | inserted_time (timestamp) |
-------------------------------------------------
| 1  | James |  xxx  |    2018-11-14 21:20:04   |
-------------------------------------------------

So when I do my select now and the current timestamp is 2018-11-15 11:20:04 I don't want my entry in my results object / array (or what ever it is). 所以当我现在选择时,当前时间戳是2018-11-15 11:20:04我不想在我的结果对象/数组中输入条目(或者它是什么)。


But when we have this current timestamp 2018-11-17 21:20:05 I need this in my results variable. 但是当我们有当前时间戳记2018-11-17 21:20:05我需要在我的结果变量中使用它。

So I'm absolutely stumped with this. 所以我对此深感困惑。 All my other stuff around this works fine but this is still a problem. 我所有与此相关的其他东西都可以正常工作,但这仍然是一个问题。

Is it possible to do this only with SQL? 是否可以仅使用SQL执行此操作? Or do I need to pass a parameter like $current_timestamp ? 还是我需要传递$ current_timestamp之类的参数?

You can use now() or current_timestamp : 您可以使用now()current_timestamp

SELECT *
FROM  my_table
WHERE inserted_time < now() - interval 3 day;

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

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