thread messaging system database schema design

前端 未结 2 908
慢半拍i
慢半拍i 2020-11-28 01:20

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

2条回答
  •  独厮守ぢ
    2020-11-28 01:50

    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
    

提交回复
热议问题