显示少于3个未答复会话的用户

时间:2017-10-13 06:41:51

标签: mysql sql database database-design

我试图让所有与其他条件少于3次未答复对话的用户接受。

但由于某种原因,它不适合我。

"用户"表

| user_id | name | email |
|---------|------|-------|
|  1      |"xxx" |"xxx"  |  
|  2      |"xxx" |"xxx"  |  
|  3      |"xxx" |"xxx"  | 
|  4      |"xxx" |"xxx"  |  
|  5      |"xxx" |"xxx"  |  
|---------|------|-------|

"会话"表

|   id    | user1| user2 |
|---------|------|-------|
|  1      |  3   |   2   |  
|  2      |  3   |   4   |  
|  3      |  5   |   2   | 
|  4      |  4   |   1   | 
|  5      |  1   |   2   |  
|---------|------|-------|

"消息"表

|   id    | conversation | sender | recipient | text |
|---------|--------------|--------|-----------|------|
|   1     |      1       |   3    |     2     |  *** |
|   2     |      2       |   3    |     4     |  *** | 
|   3     |      3       |   5    |     2     |  *** |
|   4     |      4       |   4    |     1     |  *** |
|   5     |      5       |   1    |     2     |  *** |
|   6     |      2       |   4    |     3     |  *** | 
|   7     |      4       |   1    |     4     |  *** |
|---------|--------------|--------|-----------|------|

这是我当前的查询

SELECT DISTINCT u.id,u.name, u.email 
FROM users u
INNER JOIN(
        SELECT *,COUNT(conversation) as totalConversations
        from message 
        GROUP BY id
        HAVING totalConversations IS NULL OR totalConversations < 3
    ) msg ON  (msg.sender = u.id) OR (msg.recipient = u.id)

我也尝试这个加入

LEFT JOIN
    (
       SELECT *,COUNT(id) as totalCoversations
       FROM conversation cn
            INNER JOIN(
                    SELECT conversation,COUNT(ms.conversation) as totalMsg
                    from message ms
                    GROUP BY ms.conversation
                    HAVING totalMsg < 3
            ) msg ON msg.conversation = cn.id
       GROUP BY cn.id
    ) convr ON convr.user2 = u.id 

以上查询必须返回用户1,4但不返回用户2.

1 个答案:

答案 0 :(得分:1)

首先,您必须找到所有具有相同收件人的会话 - 这意味着没有答案:

SELECT conversation, COUNT(DISTINCT recipient) numberOfRecipients, recipient
    from message 
    GROUP BY conversation, recipient 
    HAVING numberOfRecipients = 1

然后您计算未答复的对话并按收件人分组,以便了解每个用户有多少未答复的对话:

SELECT COUNT(DISTINCT t.conversation) totalUnansweredConversations, m.recipient FROM (
    SELECT conversation, COUNT(DISTINCT recipient) numberOfRecipients
    from message 
    GROUP BY conversation
    HAVING numberOfRecipients = 1
  ) t
  JOIN message m ON m.conversation = t.conversation
  GROUP BY m.recipient
  HAVING totalUnansweredConversations < 3

之后,您只需将该派生表与users表一起加入,即可获得未回复该对话的用户。完整查询:

SELECT DISTINCT users.*
FROM users
JOIN (
  SELECT COUNT(DISTINCT t.conversation) totalUnansweredConversations, m.recipient FROM (
    SELECT conversation, COUNT(DISTINCT recipient) numberOfRecipients
    from message 
    GROUP BY conversation
    HAVING numberOfRecipients = 1
  ) t
  JOIN message m ON m.conversation = t.conversation
  GROUP BY m.recipient
  HAVING totalUnansweredConversations < 3
) derived ON users.id = derived.recipient
UNION
SELECT DISTINCT u.*
FROM users u
LEFT JOIN conversation c ON u.id IN (c.user1,c.user2)
WHERE c.id IS NULL;

SQLFiddle进行测试。