yandoo Posted August 15, 2013 Share Posted August 15, 2013 Hiya I've been trying without success (and getting very strange results) to try and get my records to output only the most recent record. $sql4 = mysql_query("SELECT products.*, IF (ISNULL(p_rating.ip), 0, 1) AS HasRated FROM products LEFT JOIN p_rating ON products.id = p_rating.id AND p_rating.ip = '".$ipaddress."' WHERE category='Image' OR 'Video' ORDER BY date_added DESC LIMIT 1"); In the WHERE clause it will automatically select the last image first, even though there is a video that is more recent in the database. So I need the select statement to output the last date_added record that is either an Image or Video. If anybody could help with this that would be great. Thank you very much. Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted August 15, 2013 Solution Share Posted August 15, 2013 (edited) 1. Don't leave a space between function name and the "(". ie IF() and not IF () ). 2. The WHERE clause should be WHERE category = 'Image' OR category = 'Video', edit Alternative : WHERE category IN ('Image', 'Video') Edited August 15, 2013 by Barand Quote Link to comment Share on other sites More sharing options...
yandoo Posted August 15, 2013 Author Share Posted August 15, 2013 Thank you for your help! So am I right in thinking that Max(date_added) is not needed? I made the changes you said and it works now. Thank you so much. Quote Link to comment Share on other sites More sharing options...
Psycho Posted August 15, 2013 Share Posted August 15, 2013 So am I right in thinking that Max(date_added) is not needed? Yes. Just order the results by the relevant column and use LIMIT to get the first record. Quote Link to comment Share on other sites More sharing options...
yandoo Posted August 15, 2013 Author Share Posted August 15, 2013 Thank you very much I'm so pleased it is that way because I started thinking my understanding of dealing with the OR condition was wrong all this time. Thanks again you've made my day Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.