开发者

MySQL Where date is greater than one month?

开发者 https://www.devze.com 2023-01-25 23:14 出处:网络
I have a datetime column called \'last_login\'. I want to que开发者_如何学Cry my database to select all records that haven\'t logged in within the last month. How do I do this?

I have a datetime column called 'last_login'.

I want to que开发者_如何学Cry 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

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:

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

Naturally, as both are MySQL-specific this limits you to MySQL backends. 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.


matthewh was almost correct, except the > should have been a right one.

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

0

精彩评论

暂无评论...
验证码 换一张
取 消