I have three tables, user, conversation, and conversation_participant with the rows:
user
id
conversation
idtype
conversation_participant
conversation_iduser_id
(type indicates that it is a “private,” 1-1 conversation, i.e., ensuring that user1 and user2’s conversation is delineated from a group conversation involving, for example, user1, user2, and user3. type=0 is private, type=1 is a group.)
Regardless, how can I best structure a query that determines if a private conversation between two users (user_id=1 and user_id=2) exists? I am tempted to do something like this (I am very new to SQL, mind you):
SELECT conversation_participant.conversation_id FROM conversation_participant WHERE user_id=1 …but I’m pretty certain this is off to an unfortunate start.
What is the most concise way to manage a situation like this with queries from multiple tables that are all interconnected in this fashion? I assume it’s relatively simple, and if there are any good readings on this that you may know of, they would be much appreciated as well.
Thank you!
If I understand correctly that you are looking for conversations between two specific known user ids:
In English, find ids of conversations where:
user_id 1 is a participant (from and where clauses)
and user_id 2 is a participant in the same conversation (first join clause)
and that conversation is of private type (second join clause).