Jump to content

Add MAX(date) to left joined table


Go to solution Solved by Barand,

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

  • Solution

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 by Barand
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.