I have a personal message system on my website, which is pretty easy.
But I would like to have an administrator page where all conversations are shown between users and their amount of messages.
So the table looks like (simplified version):
CREATE TABLE pm (
id INT(10) NOT NULL AUTO_INCREMENT,
from INT(10) NOT NULL REFERENCES (usertable),
to INT(10) NOT NULL REFERENCES (usertable),
message BLOB NOT NULL
);
Example:
Say I have some users: Mark, John, Bryan and Kate.
Mark (from) sends 5 messages to John (to) and John (from) sends 3 messages to Mark (to).
Kate (from) sends 2 messages to Bryan (to) and Bryan (from) sends 1 message to Kate (to).
I would like a result set that shows
Mark – John – 8 messages
Kate – Bryan – 3 messages
And this for all users in my table at once.
I’m really stuck on this and I have searched everywhere but have not found a solution.
The difficulty lies in the fact that I want all users listed and I have to cross the "from" and "to" column somehow…
I hope anyone will be able to help.
Thanks in advance.
Full sample
To turn the IDs into names, use the normal to
usertable or some such. e.g.