简体   繁体   English

选择其他字段上具有相同值的行

[英]Select rows with same value on other field

So, i have an entity (i'm using symfony) Participant . 因此,我有一个实体(我正在使用symfony) Participant

In this entity i have : id account_id conversation_id 在这个实体,我有: id account_id conversation_id

account_id is the User object, and conversation_id is the Conversation object, both are foreign keys. account_idUser对象, conversation_idConversation对象,两者都是外键。

I need to know if, for start, user1 and user2 are in the same conversation. 首先,我需要知道user1和user2是否在同一会话中。 And after i need to know if user1, user2.... userN are in the same conversation. 在我需要知道user1,user2 ...... userN是否在同一对话中之后。

I don't know how to do that simply with a query ? 我不知道如何通过查询来做到这一点? Thanks ! 谢谢 !

SELECT
   n.*
FROM
   Participant n JOIN
(
  SELECT 
      t.conversation_id
  FROM
      Participant t
  WHERE 
          t.user_id = 'user1' 
      AND 
          t.conversation_id = (SELECT conversation_id FROM Participant WHERE user_id = 'user2' AND conversation_id = t.conversation_id)
) m on m.conversation_id = n.conversation_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM