开发者

My SQL grab last active users within one week

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜