Jump to content

Add MAX(date) to left joined table


yandoo

Recommended Posts

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. 

 

Link to comment
https://forums.phpfreaks.com/topic/281217-add-maxdate-to-left-joined-table/
Share on other sites

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')

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.