0

Apologies for the poorly worded question.

I have two tables, called conversations and conversation_users. Each conversation can have multiple users attached to it. The fields are as follows:

conversations

  • conv_id
  • conv_author_id
  • conv_date

conversation_users

  • cu_id
  • cu_conv_id
  • cu_user_id

Now, to avoid duplication of conversations with the same users. I want to check whether a conversation already exists with users with ID, say 12 and 25. If one exists, I wish to obtain the ID of that conversation.

How do I go about structuring a query to do this?

4
  • MySQL, Unless you meant something else? :p Commented Nov 6, 2013 at 11:56
  • How are the multiple users stored on the conversations table? Are there multiple rows with the same conv_id and different conv_author_id values? Commented Nov 6, 2013 at 11:56
  • All users participating in a conversation are stored in conversation_users. The author_id is just to track who started the conversation. Commented Nov 6, 2013 at 11:57
  • @emkay just saw that you added the tag before answering my comment Commented Nov 6, 2013 at 11:57

1 Answer 1

3

I want to check whether a conversation already exists with users with ID, say 12 and 25.

You can use EXISTS:

SELECT CASE WHEN EXISTS
(
    SELECT 1 FROM conversation_users cu
    WHERE cu.cu_conv_id = c.conv_id
    AND   cu.cu_user_id IN (12, 25)
) THEN 'Yes, these users are in this conversation' 
  ELSE 'This conversation is between different users' END AS Result
FROM conversations c
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.