Need help with a request

I have the following table. It stores messages that each user sent and received in chats. I want to select all the messages that the user sent and received, and receive them. Then I can sort them by their conversation id in JQUERY. Should I make two queries for this or can I embed the query? Essentially, I want to do

SELECT * FROM chatbox WHERE sender=? 
and 
SELECT * FROM chatbox WHERE receiver=?

I just want to try to avoid two queries and two loops for the results. Anyone have any suggestions?

+3
source share
4 answers
SELECT 
  *
FROM 
  chatbox
WHERE
  sender=? or receiver=?
+1
source

You can do it like this, and also make an order with MySQL too:

SELECT *
FROM chatbox
WHERE
  sender="<id>" OR
  receiver="<id>"
ORDER BY conversation_id
+3
source

You can do this in one query along with sorting;

SELECT * FROM chatbox WHERE 'username' IN (sender, receiver) ORDER BY id
+2
source
SELECT * FROM chatbox WHERE sender=? OR receiver=?
+1
source

All Articles