I have the following table. It stores messages that each user has sent and received in chats. I want to select all the messages that a user has sent and received and retrieve them. I can then sort them by their conversation id in JQUERY. Would I have to do two querie开发者_如何学JAVAs for this or can I embed a query? Essentially what I want to do is
SELECT * FROM chatbox WHERE sender=?
and
SELECT * FROM chatbox WHERE receiver=?
I just want to try to avoid having two queries and two while loops for the results. Anyone have any suggestions?
You can do it like this, and also do the ordering with MySQL too:
SELECT *
FROM chatbox
WHERE
sender="<id>" OR
receiver="<id>"
ORDER BY conversation_id
You can do it in a single query along with the sorting;
SELECT * FROM chatbox WHERE 'username' IN (sender, receiver) ORDER BY id
SELECT
*
FROM
chatbox
WHERE
sender=? or receiver=?
SELECT * FROM chatbox WHERE sender=? OR receiver=?
精彩评论