I have a chat system that works fine, but the message list I've just realised pulls every message per person then displays the last result, which obviously is a massive waste of resources and stress on the server to return only one result per user.
This is the query to retrieve the message list, and afterwards there is a while loop that cleans the array to show only the latest message per user, I know there is a much better way to do this using JOIN but I can't get my head around it.
$result = dbconstruct("SELECT messages.id, messages.msgfrom, messages.msg,
messages.active, messages.replied, users.username, users.online, users.admin,
users.imagename FROM messages, users WHERE msgto='$_SESSION[id]'
AND users.id=msgfrom ORDER by messages.id DESC");
I have looked through other answers, but as stated, finding it quite hard to grasp the join techniques.
update
I don't think I've explained what I need very well. Currently I run the above query, return about 2000 results and then use this to filter each users message to a single message.
$clean = array(); while($result->fetch_assoc()) { if(in_array($result[msgfrom],
$clean)) { }
else { //print message }
$usercheck = array_push($clean, $result[msgfrom]); }
This displays the message list fine and it shows only the last message from each user, but is a very bad way to do so. There must be a way to do this inside the SQL query.