简体   繁体   English

复杂的MySQL会话组查询?

[英]Complex MySQL Conversation Group Query?

I have the following tables. 我有下表。

conversations
| id |
------
  1

and

conversationMembers
| id | conversationId | userId | email
---------------------------------------
   1         1            2      null  
   2         1           null    test@test.com
   3         1            7      null

Basically, I'm trying to construct a MySQL query that returns a row from the conversations table by an exact match of conversationMembers. 基本上,我正在尝试构造一个MySQL查询,该查询通过对sessionMembers的完全匹配从对话表返回一行。

So, here's some examples of expected returns. 因此,这里有一些预期收益的例子。

Let's say we want aa conversation id for a conversation between the exact following members: userId 2, userId 7, email test@test.com - This would see that in the conversationMembers table there's rows with the same conversation id and the exact match across all members of that conversation id that we're searching for. 假设我们想要以下确切成员之间的对话的对话ID:userId 2,userId 7,电子邮件test@test.com-这将看到,在sessionMembers表中,行具有相同的对话ID,并且所有行之间都完全匹配我们正在搜索的对话ID的成员。 It would return conversations row with id 1. 它将返回ID为1的对话行。

Here's another example. 这是另一个例子。 We want a conversation id for a conversation between userId 2 and userId 7. This would see that there's not a conversation exclusively between userId 2 and userId 7, so it would not return anything. 我们需要一个userid 2和userId 7之间的对话的对话ID。这将看到在userId 2和userId 7之间没有专门的对话,因此它不会返回任何内容。

And a final example. 最后一个例子。 Let's say we want userId 7 and userId 9, this would also see there's no exclusive conversation between these 2 user id's and would return nothing. 假设我们想要userId 7和userId 9,这还将看到这两个用户ID之间没有互斥的对话,并且不会返回任何内容。

What's the best way to go about doing it? 最好的方法是什么? I've played with subqueries but everything I've come up with doesn't seem to be able to handle the exact matching situation - I was having issues with selecting conversations for example - on userId 2 and 7 only (which should return nothing) and was getting conversationId 1 back, even though I didn't specify I wanted a conversation with test@test.com email as a part of it. 我玩过子查询,但是我提出的所有内容似乎都无法处理确切的匹配情况-例如,在选择对话时遇到了问题-仅针对userId 2和7(应该不返回任何内容)即使没有指定我希望通过test@test.com电子邮件进行对话,也可以重新获得对话ID 1。 I should only have gotten conversationId 1 back for if I searched on an exact match of all members in for conversationId. 如果我在对话中搜索所有成员的完全匹配项,则应该只返回了sessionId 1。

One method is to use group by and having . 一种方法是使用group byhaving This is nice because it is flexible with regards to what can be expressed. 这很好,因为它可以灵活表达。 So, your first example is: 因此,您的第一个示例是:

select conversionid
from conversationMembers
group by conversionid
having sum(userId = 2) > 0 and
       sum(userId = 7) > 0 and
       sum(email = 'test@test.com') > 0;

The condition being summed counts the number of members that match. 被求和的条件计算匹配的成员数。 The > 0 means there is at least one. > 0表示至少有一个。 For the second condition, the clause would be: 对于第二个条件,该子句为:

having sum(userId = 2) > 0 and
       sum(userId = 7) > 0 and
       sum(userId not in (2, 7)) = 0;

or alternatively: 或者:

select conversionid
from conversationMembers
group by conversionid
having sum(userId = 2) > 0 and
       sum(userId = 7) > 0 and
       count(distinct userId) = 2;

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

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