I’ve got a database table called ‘mesg’ with the following structure:
receiver_id | sender_id | message | timestamp | read
Example:
2 *(«me)* | 6 *(«nice girl)* | 'I like you, more than ghoti' | yearsago | 1 *(«seen it)*
2 *(«me)* | 6 *(«nice girl)* | 'I like you, more than fish' | now | 1 *(«seen it)*
6 *(«nice girl)* | 2 *(«me)* | 'Hey, wanna go fish?' | yearsago+1sec | 0 *(«she hasn't seen it)*
It’s quite a tricky thing that I want to achieve.
I want to get: the most recent message(=ORDER BY time DESC) + ‘contact name’ + time for each ‘conversation’.
- Contact name = uname WHERE uid = ‘contact ID’ (the username is in another table)
- Contact ID = if(sessionID*(«me)*=sender_id){receiver_id}else{sender_id}
- Conversation is me = receiver OR me = sender
For example:
From: **Bas Kreuntjes** *(« The message from bas is the most recent)*
Hey $py, How are you doing...
From: **Sophie Naarden** *(« Second recent)*
Well hello, would you like to buy my spam? ... *(«I'll work on that later >p)*
To: **Melanie van Deijk** *(« My message to Melanie is 3th)*
And? Did you kiss him? ...
That is a rough output.
QUESTION: Could someone please help me setup a good SQL command.
This will be the while loup
<?php
$sql = "????";
$result = mysql_query($sql);
while($fetch = mysql_fetch_assoc($result)){ ?>
<div class="message-block">
<h1><?php echo($fetch['uname']); ?></h1>
<p><?php echo($fetch['message']); ?></p>
<p><?php echo($fetch['time']); ?></p>
</div>
<?php } ?>
I hope my explanation is good enough, if not, please tell me.
Please don’t mind my English, and the Dutch names (I am Dutch myself)
Feel free to correct my English
UPDATE1: Best I’ve got until now:
But I do not want more than one conversation to show up…
u=user table
m=message table
SELECT u.uname, m.message, m.receiver_uid, m.sender_uid, m.time
FROM m, u
WHERE (m.receiver_uid = '$myID' AND u.uid = m.sender_uid)
OR (m.sender_uid = '$myID' AND u.uid = m.receiver_uid)
ORDER BY time DESC;
Fiddly…but doable. Build the answer up one piece at a time.
Constructing the query – step by step
Given a particular reference user (‘me’) in the examples, you need to find all messages between ‘me’ and another person. For each such other person, you want to find the one message with the most recent time stamp.
Query 1
Let’s get a conversation ID established, linking all messages together. Since we’re interested in one party (‘me’) at a time, we can use the other person’s ID to identify the conversation.
That allows you to thread all the chats with an individual together with the same conversation ID.
Query 2
Now you need to group that by conversation to find the conversation and the most recent time; for this, you don’t need the other (recipient_uid or sender_uid) columns:
Query 3
So, for each conversation, we now know the most recent timestamp. We just have to join that information back with the previous query to get the majority of the details:
Query 4
Since you said you wanted names as well, you can extend that to join with the users table twice:
There – that’s fun. I wouldn’t want to write that in one go, but built up a piece at a time, it isn’t too intimidating (though it isn’t trivial).
Test Schema
User table
Mesg Table
DATETIME YEAR TO SECOND is a funny way of writing TIMESTAMP (in IBM Informix Dynamic Server – which is where I tested this).
User Data
Mesg Data
Results 1
Results 2
Results 3
Results 4
Wow, it isn’t often I get a piece of SQL that is this complex right on the first go, but on this occasion, apart from a trip up over receiver_uid vs recipient_uid in the first iteration, it did work correctly first time.
General Solution
Omitting the ‘who’ (or ‘me’, or ‘$myID’) parameter, we can come up with a general solution for the latest message in any of the conversations between two people. The conversation is identified by the higher and lower (or vice versa) participant ID. Otherwise, it is very similar to the previous one.
Result with sample data