Jump to content

[SOLVED] grouping question


smerny

Recommended Posts

	$search = "SELECT * FROM messages WHERE ID_to = '".$user_id."' GROUP BY ID_convo ORDER BY time DESC";
$result = mysql_query($search) or die ("SQL Error: ". mysql_error());
while ($messages = mysql_fetch_array($result))
{
	$search = "SELECT DisplayName FROM users WHERE ID = '".$messages['ID_from']."' LIMIT 1";
	$result2 = mysql_query($search) or die ("SQL Error: ". mysql_error());
	$users = mysql_fetch_array($result2);

	echo "<tr>
	<td>". $users['DisplayName'] ."</td>
	<td>". $messages['subject'] ."</td>
	<td>". $messages['time'] ."</td>";
}

 

This is only giving me the information from the FIRST message with the message convo ID, I want it to only give me the LAST message with the message convo ID

Link to comment
Share on other sites

Hi

 

Think you need some SQL similar to this to get the latest record.

 

SELECT *

FROM (SELECT ID_convo, MAX(time) AS msgTime FROM messages WHERE ID_to = '".$user_id."' GROUP BY ID_convo) a

INNER JOIN messages b

ON a.ID_convo = b.ID_convo

AND a.msgTime = b.time

 

All the best

 

Keith

Link to comment
Share on other sites

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.