1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

MySQL Query Finding rows since last [day of week]

Discussion in 'MySQL' started by tflight, Apr 21, 2008.

  1. #1
    Looking for some help with a MySQL query. I have a table with a field containing a timestamp value (such as 2008-04-21 10:12:04). I want a query that will return rows from that table where the timestamp is greater than the most recent Saturday.

    For example, today is Monday so the were clause would include all records where the timestamp was on Saturday (two days ago), Sunday, or today.

    If today was a Thursday, the were clause would include records from the previous Sat, Sun, Mon, Tue, Wed, and Thu.

    If today was a Saturday, get records created that day.

    So I basically need to figure out the date of the most recent Saturday, and have the where clause fetch all records starting from 00:00:00 on that day.
     
    tflight, Apr 21, 2008 IP
  2. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #2
    SELECT * FROM table WHERE 
    timestamp_field >= CAST( CONCAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(CURDATE()-5) DAY), ' 00:00:00') AS DATETIME)
    Code (markup):
     
    mwasif, Apr 21, 2008 IP
    tflight likes this.
  3. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Brilliant, thanks. I'll be sure to check it out to see how it works.
     
    tflight, Apr 21, 2008 IP
  4. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #4
    Waiting for the response:)
     
    mwasif, Apr 21, 2008 IP
  5. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry, what I meant was "brilliant, thanks, it works perfectly" and "I'll be sure to check it out and see why it works so I can learn."

    Cheers!
     
    tflight, Apr 22, 2008 IP
  6. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #6
    Ohhh, thanks for the comments :)
     
    mwasif, Apr 22, 2008 IP
  7. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hmmm.... perhaps this isn't working across new months as it appears to have broken this morning.
     
    tflight, May 1, 2008 IP
  8. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #8
    Fixed query
    SELECT * FROM table WHERE 
    timestamp_field >= CONCAT( DATE_SUB(CURDATE(), INTERVAL WEEKDAY(DATE_SUB(CURDATE(), INTERVAL 5 DAY)) DAY), ' 00:00:00');
    Code (markup):
     
    mwasif, May 2, 2008 IP
  9. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Hey, that works better, thanks again!
     
    tflight, May 2, 2008 IP
  10. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #10
    You are welcome.
     
    mwasif, May 2, 2008 IP