简体   繁体   English

MySQL哪里的日期大于一个月?

[英]MySQL Where date is greater than one month?

I have a datetime column called 'last_login'. 我有一个名为'last_login'的日期时间列。

I want to query my database to select all records that haven't logged in within the last month. 我想查询我的数据库以选择上个月内未登录的所有记录。 How do I do this? 我该怎么做呢?

This is what I have currently: 这就是我目前所拥有的:

$query = $this->query("SELECT u.id, u.name, u.email, u.registered, g.name as group_name FROM `:@users` AS u LEFT JOIN `:@groups` AS g on u.group_id = g.id WHERE u.last_login = ...... LIMIT {$limit_start}, {$limit_end}");

:@ = database prefix :@ =数据库前缀

Try using date_sub 尝试使用date_sub

where u.last_login < date_sub(now(), interval 1 month)

(Similar to the first answer but in my mind it is more "natural" to use positive integers) (与第一个答案类似,但在我看来,使用正整数更“自然”)

You can use date_add combined with now : 您可以将date_addnow结合使用:

...where u.last_login < date_add(now(), interval -1 month)

Naturally, as both are MySQL-specific this limits you to MySQL backends. 当然,由于两者都是特定于MySQL的,因此限制了MySQL后端。 Alternately, you can figure out what the date was a month ago with PHP (I'm not a PHP person, but I'm guessing DateTime::sub would help with that) and then include that date in your query in the normal way you would any other date/time field. 或者,您可以通过PHP找出一个月前的日期(我不是PHP人员,但我猜测DateTime::sub会对此有所帮助)然后以正常方式在查询中包含该日期你会得到任何其他日期/时间字段。

matthewh几乎是正确的,除了>应该是正确的。

where u.last_login > date_sub(now(), interval 1 month)

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

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