I have this message table that allows users to send messages to one another. sender_id is the id of the person who sent the message. receiver_id is the id of the person who received the message. message_id adds the sender_id and receiver_id together. This is a sample below.
id sender_id receiver_id message_id message time
1 2 7 9 hi -
2 7 2 9 hello
3 4 2 6 good
4 2 4 6 bad
Now, I am trying to group the messages together using message_id by doing this.
Note: $_SESSION['id'] is '2'
$query = "SELECT * FROM message WHERE sender_id = :id OR receiver_id = :id GROUP BY message_id HAVING COUNT(*) >=1 ORDER BY time DESC";
foreach ($db->query($query, array('id' => $_SESSION['id'])) AS $output)
{
$message = $output['message'];
}
What displays is:
hi
good
But I want the query to show the latest messages and not the first messages i.e.
hello
bad
Please, how do I do this?