Query on Today and Days Before Today

Discussion in 'MySQL' started by adbox, Oct 19, 2009.

  1. #1
    Hey guys,

    I am trying to update rows that have a status "future" for today and all days before today. Here is what I am using so far:

    But it seems to not work. Is there a better way to do this?
     
    adbox, Oct 19, 2009 IP
  2. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #2
    Well I was doing something wrong, but the query above works a-o-k.
     
    adbox, Oct 19, 2009 IP
  3. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #3
    What type of data is the column `post_date`?

    Assuming that post_date is a date type column, this query may be doing more harm than good...

    DATE(post_date)<='$date_today'
     
    jestep, Oct 19, 2009 IP
  4. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #4
    it is a date type collumn, what are your suggestions?
     
    adbox, Oct 19, 2009 IP
  5. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #5
    You should be able to simply use:

    WHERE post_date <= '$date_today'

    There's 2 problems that you would experience using WHERE DATE() or WHERE FUNCTION()..

    First off, if you use any function: CAST, MAX, DATE, TIME, etc... in the where clause, MySQL cannot utilize an index on the column. This may or may not be an issue depending on whether you have an index on the column or not. If there is an index, it is ignored.

    Second problem, is that the function could be applied to the entire column before any sorting will be performed. If your database has 100 records, it's essentially negligible, but performing DATE() on 10,000,000 records would create a huge amount of overhead for a simple date comparison. Same thing goes with any function in the where clause.
     
    jestep, Oct 19, 2009 IP
  6. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #6
    thanks a ton man. I figured the the two dates had to be in the same long format to work, and what you said makes sense, so I'll just clip that DATE() right off.
     
    adbox, Oct 19, 2009 IP