Messaging System with PHP/MySQL

前端 未结 3 1834
囚心锁ツ
囚心锁ツ 2021-01-21 11:46

Hi I\'m trying to make a messaging system with php and mysql.

The mysql table is simple: id sender receiver text timestamp

I\'m trying to make the messaging some

相关标签:
3条回答
  • 2021-01-21 12:11

    DESC for receive New row's . AND ASC or NOT set, Default is ASC:

       (SELECT * FROM messages WHERE receiver = 13 OR sender = 13 GROUP BY receiver,sender ORDER BY id DESC)
    

    AND SET LIMIT 1 ,1 AFTER ORDER BY
    i think you need it
    (receiver = 13 AND sender = 'usersender id' ) OR (receiver ='usersender id' AND sender = 13 ) Try it!

    0 讨论(0)
  • 2021-01-21 12:19

    UPDAte: I'm not sure if it works perfect:

    SELECT * FROM messages 
    WHERE receiver = 13
    GROUP BY receiver,sender 
    ORDER BY timestamp DESC
    LIMIT 1
    
    UNION ALL
    SELECT * FROM messages 
    WHERE sender = 13
    GROUP BY receiver,sender 
    ORDER BY timestamp DESC
    LIMIT 1
    

    to reverse the order:

    ORDER BY timestamp DESC
    
    0 讨论(0)
  • 2021-01-21 12:25
    SELECT messages.* FROM messages, (SELECT MAX(id) as lastid FROM messages 
    WHERE receiver = 13 OR sender = 13 
    GROUP BY CONCAT(LEAST(receiver,sender),'.',GREATEST(receiver,sender))) as conversations
    WHERE id = conversations.lastid
    ORDER BY timestamp DESC
    

    what you need is a unique conversation id between the chat-partners. i've simulated this with the subquery, hope this helps

    0 讨论(0)
提交回复
热议问题