How would I write this query?

Discussion in 'MySQL' started by karl_murphy, Jul 12, 2010.

  1. #1
    Hello all,

    I have a basic table within a database called learner_users, which stores the account details of each user.

    How would I write a query to find all of the users who haven't logged in for 100 days. There is a field within the table called last_login.

    Thanks.
     
    karl_murphy, Jul 12, 2010 IP
  2. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #2
    What type is last_login field? It's better to post your create table script here.

    Regards :)
    p.p.: I mean date, datetime, timestamp, integer...?
     
    koko5, Jul 12, 2010 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    Assuming it is any standard date column and not a varchar you could do something like this.

    
    
    SELECT column_names FROM learner_users 
    WHERE  last_login <= DATE_SUB(CURDATE(),INTERVAL 100 DAY);
    Code (markup):
     
    jestep, Jul 12, 2010 IP
  4. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    The last_login field has a data type of datetime.

    A typical value in this field for a user would be something like:

    "2010-06-11 17:05:07"

    Hope this helps.

    Thanks in advance
     
    karl_murphy, Jul 13, 2010 IP
  5. koko5

    koko5 Active Member

    Messages:
    394
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    70
    #5
    Hi,
    you can use query, provided by @Jestep
    If you don't want to take care about hours,minutes and seconds and compare dates only, just pass last_login in DATE function, so query will looks like:
    
    SELECT column_names FROM learner_users 
    WHERE [COLOR="Red"]DATE[/COLOR](last_login) <= DATE_SUB(CURDATE(),INTERVAL 100 DAY);
    
    Code (markup):
    otherwise
    
    SELECT column_names FROM learner_users 
    WHERE last_login <= DATE_SUB([COLOR="Red"]NOW()[/COLOR],INTERVAL 100 DAY);
    
    Code (markup):
    Regards :)
     
    koko5, Jul 13, 2010 IP
    karl_murphy likes this.
  6. karl_murphy

    karl_murphy Member

    Messages:
    82
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    This works great, thanks!
     
    karl_murphy, Jul 19, 2010 IP