I\'m trying to achieve exactly what\'s explained here: Creating a threaded private messaging system like facebook and gmail, however i don\'t completly understand Joel Bro
According to Joel Brown'answer, you can add LAST_MESSAGE_ID
column into THREAD table then getting all threads with last messages SQL is become very simple. You must update this column when every message send.
Getting all threads with latest message in each for a given user
SELECT *
FROM THREAD T
INNER JOIN MESSAGE M ON T.LAST_MESSAGE_ID=M.MESSAGE_ID
INNER JOIN USER SENDER ON M.USER_ID=SENDER.USER_ID
LEFT JOIN MessageReadState MRS ON M.MESSAGE_ID=MRS.MESSAGE_ID AND MRS.USER_ID=2