开发者

My SQL grab last active users within one week

开发者 https://www.devze.com 2023-02-02 03:06 出处:网络
I have a query and I\'m not sure how to limit the users by last login date.i only want to return the users who have logged 开发者_StackOverflow中文版in in the last week. Here\'s my query:

I have a query and I'm not sure how to limit the users by last login date. i only want to return the users who have logged 开发者_StackOverflow中文版in in the last week. Here's my query:

"SELECT `ID` FROM `Profiles` WHERE `DateLastLogin` < '[1WEEK]'"

The DateLastLogin is a datetime field.


WHERE DateLastLogin > ADDDATE(NOW(), INTERVAL -1 WEEK)


You can do this:

SELECT `ID` FROM `Profiles` WHERE `DateLastLogin` < DATE_SUB(NOW(), INTERVAL 7 DAY)

Or

SELECT `ID` FROM `Profiles` WHERE `DateLastLogin` < DATE_SUB(NOW(), INTERVAL 1 WEEK)


"SELECT `ID` FROM `Profiles` WHERE `DateLastLogin` < DATE_SUB(curdate(), INTERVAL 1 WEEK)"

Probably the query you are looking for is

"SELECT `ID` FROM `Profiles` WHERE `DateLastLogin` BETWEEN NOW() AND DATE_SUB(curdate(), INTERVAL 1 WEEK)"


In PHP you can use

strtotime('-1 week');

and that will output the unix time from one week ago


SELECT Count(* ) FROM Users WHERE CreatedDate > DATEADD(DAY, -7, GETDATE())
0

精彩评论

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

关注公众号