MySQL query for date

Discussion in 'PC Help Desk' started by appcomm, Jun 13, 2011.

  1. appcomm

    appcomm Well-Known Member

    Looking for a date function to run in selecting records in a MySQL database.

    The task is, no matter when the query is run, need it to select records for a specific DAY in the past. In this case, I want it to always select records that were added the previous Saturday. So if I run this query on Monday, it returns records from the previous Saturday....if I run the query on Thursday, it returns records from the previous Saturday, etc.

    I have it in Oracle, but not quite making the transition to work in MySQL:

    The "dual" is Oracle specific, so obviously doesn't work in MySQL. So, in a nutshell, is there a "Last Saturday" SQL function I can use for this?

    TIA for any suggestions.
     
  2. JayT

    JayT Well-Known Member

    It seems that if you combined DATEDIFF() and DAYOFWEEK() functions, you could get that functionality. Let me build a quick MySQL table and run a few queries to test this...
     
  3. ServerSnapper

    ServerSnapper Well-Known Member

    Run an If statement

    If ($to_date){

    $query = "SELECT * FROM table WHERE date > '".$from_date."' AND < '".$to_date."';
    }else{
    $query = "SELECT * FROM table WHERE date = '".$from_date."'";
    }
    $result = mysql_query($query);
     
  4. JayT

    JayT Well-Known Member

    select * from <insert table name here> where dayofweek(dateline) = 7 and datediff(dateline, now()) <7

    Seemed to work for me... ("dateline" is also my field name, you'll have to substitute yours here too)
     
  5. appcomm

    appcomm Well-Known Member

    Thanks! That points me in the right direction!
     
  6. JayT

    JayT Well-Known Member

    np! glad it helped!
     
  7. ServerSnapper

    ServerSnapper Well-Known Member

    You know these answers are worth 75.00 to 100.00 worth of Gift cards!
     
  8. CAC

    CAC Well-Known Member

    SS, maybe you should start an advisory service. 8)
     
  9. ServerSnapper

    ServerSnapper Well-Known Member


    Sorry your think tank blows dust.
     
  10. CAC

    CAC Well-Known Member

    Gold dust? 8)
     

Share This Page