I’m having trouble finding a right title for this.
I’m working on a messaging system with two tables, one with all the different conversations (users in conversation and uid of the conversation) and one that contains the messages between the users.
For each conversation I’m creating a row for every user in the conversation with the uid of the conversation and the user_id. In conversations there can be just 2 users but also many more (3,4,5,6 etc.)
I’m having a problem with my SQL statement that I don’t know how to solve. The problem is when lets say that there is a conversation between 3 user, after that lets say that 2 of the users want to start talking to each other in private without the 3rd user. How can I make the SQL statement return nothing when the 2 users are already in a conversation together with other users. So that I can add 2 new rows in my table with their own private conversation_id.
the conversation_list table looks like this :
|id | user_id| conversation_id|
-----------------------------
| 1 | 1| 1|
| 2 | 3| 1|
| 3 | 1| 2|
| 4 | 2| 2|
| 5 | 4| 2|
And for now my SQL code is like this :
SELECT *
FROM conversation_list
WHERE user_id IN (user1_id,user_id2)
GROUP BY conversation_id
HAVING COUNT(*) = 2
Is this restatement of your question correct:
“How can I make the SQL statement return nothing when any of the two users are in a conversation with any other users”?
…or
“How can I make the SQL statement return rows ONLY when these users are NOT in a conversation with other users?”
This should only return rows if these users are NOT in a conversation with other users.