I am trying to implement a message system quite similar to facebook . The message table is :
+--------+----------+--------+-----+----------+
| msg_id | msg_from | msg_to | msg | msg_time |
+--------+----------+--------+-----+----------+
Here msg_from and msg_to contain user ids and the msg_time contains the timestamp of the message . A user’s user id can appear in both the to and from column and multiple times for another user . How should I write a SQL query which selects the most recent sent message between two users ? (The message can come from either one) 1 to 2 or 2 to 1 .
Since John Woo clarified that it is not directional, here’s my new answer:
Output:
For this input:
SQLFiddle Demo
If ANSI SQL is your cup of tea, here’s the way to do it: http://sqlfiddle.com/#!2/0a575/19