[英]MySQL query to return rows that are equal to or greater than a certain date, where the date is separated in year, month and day columns
[英]MySQL Where date is greater than one month?
我有一个名为'last_login'的日期时间列。
我想查询我的数据库以选择上个月内未登录的所有记录。 我该怎么做呢?
这就是我目前所拥有的:
$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}");
:@ =数据库前缀
尝试使用date_sub
where u.last_login < date_sub(now(), interval 1 month)
(与第一个答案类似,但在我看来,使用正整数更“自然”)
...where u.last_login < date_add(now(), interval -1 month)
当然,由于两者都是特定于MySQL的,因此限制了MySQL后端。 或者,您可以通过PHP找出一个月前的日期(我不是PHP人员,但我猜测DateTime::sub
会对此有所帮助)然后以正常方式在查询中包含该日期你会得到任何其他日期/时间字段。
matthewh几乎是正确的,除了>
应该是正确的。
where u.last_login > date_sub(now(), interval 1 month)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.