I have two tables, one that stores incoming messages and one that stores outgoing messages. What I would like is to be able to have a conversations view of the messages so that all incoming and outgoing messages from/to the same user id are grouped and the conversations are ordered by the most recent message (in or out)
Outgoing
----------
user_id
time
message
Incoming
----------
user_id
time
message
What I would like is to display the results such as
-> User A 9:10 pm Nice ...
<- User A 8:45 pm Our special is pepperoni!
-> User A 8:00 pm What's your special dish?
<- User B 9:00 pm We open at 5
-> User B 6:56 pm Hello What time to you open?
<- User C 8:43 pm Thanks!
-> User C 4:00 pm Loved the pizza today!!
Any idea how to write a query to do this?
EDIT
If user B then texts back in, the result should be:
-> User B 9:15 pm Ok great!
<- User B 9:00 pm We open at 5
-> User B 6:56 pm Hello What time to you open?
-> User A 9:10 pm Nice ...
<- User A 8:45 pm Our special is pepperoni!
-> User A 8:00 pm What's your special dish?
<- User C 8:43 pm Thanks!
-> User C 4:00 pm Loved the pizza today!!
You need to
UNIONthe two tables and sort (ORDER BY) accordingly:After the additional explanations for the complex ordering: