adding DATE_SUB to query to return range of values in mysql
        Posted  
        
            by ian
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by ian
        
        
        
        Published on 2010-06-16T06:58:28Z
        Indexed on 
            2010/06/16
            7:02 UTC
        
        
        Read the original article
        Hit count: 359
        
Here is my original query:
$query = mysql_query("SELECT s.*, UNIX_TIMESTAMP(`date`) AS `date`, f.userid as favoritehash FROM songs s LEFT JOIN favorites f ON f.favorite = s.id AND f.userid = '$userhash' ORDER BY s.date DESC");
This returns all the songs in my DB and then joins data from my favorites table so I can display wich items a return visitors has clicked as favorites or not. Visitors are recognized by a unique has storred in a cookie and in the favorites table.
I need to alter this query so that I can get just the last months worth of songs. Below is my attempt at adding DATE_SUB to my query:
$query = mysql_query("SELECT s.*, UNIX_TIMESTAMP(`date`) AS `date`, f.userid as favoritehash FROM songs s WHERE `date` >= DATE_SUB( NOW( ) , INTERVAL 1 MONTH ) LEFT JOIN favorites f ON f.favorite = s.id AND f.userid = '$userhash' ORDER BY s.date DESC");
Suggestions?
© Stack Overflow or respective owner