Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL select all users who have not logged in for 30 days

I have a table that has a user id & timestamp, like the following.

[member id] [timestamp]
1            1142461087
1            1339461211
2            1321280124
1            1249042100
3            1308325002
3            1308259235

I want to return a list of unique member ids who have not logged in for 30 days or longer.

This is my current query:

SELECT member_id, login_timestamp
  FROM logins 
  WHERE login_timestamp <= 1329461087
  GROUP BY member_id
  ORDER BY login_timestamp DESC

I thought this was correct, but for some reason, in production, the admin user's ID shows up on the list, which is not possible since admin logs every day.

like image 790
Kristian Avatar asked Dec 13 '22 04:12

Kristian


1 Answers

Something like this should work.

SELECT member_id, MAX(login_timestamp)
FROM logins 
GROUP BY member_id
HAVING MAX(login_timestamp) <= 1329461087
ORDER BY MAX(login_timestamp) DESC
like image 86
Kibbee Avatar answered Dec 30 '22 11:12

Kibbee