2017-10-13 60 views
-1

我正在嘗試讓所有用戶少於3個未回答的對話與其他一些條件。顯示用戶少於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

+0

定義_unanswered_。 – jarlh

+0

未答覆的對話意味着:只有一封發件人發送給收件人但未收到收件人回覆的郵件的會話。 – DMH

+0

首先,您需要一個返回那些未答覆的對話的子查詢。然後你加入並分組。 – jarlh

回答

1

首先,你必須找到所有的對話內容有同樣的收件人 - 這意味着沒有答案:

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

然後,你數不清答案ersations和組由收件人讓你獲得每個用戶擁有多少懸而未決的對話:

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 

之後,你剛剛加入與用戶表派生表,你會得到誰沒有回答會話的用戶。完整的查詢:

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

+0

我得到這個錯誤 '語法錯誤或訪問衝突:1055 SELECT列表的表達式#3不在GROUP BY子句中並且包含非聚集列'消息。收件人',它在功能上不依賴於GROUP BY子句中的列;這與sql_mode = only_full_group_by' – DMH

+0

不兼容我已將「收件人」添加到GROUP BY。現在試試。 – wast

+0

錯誤消失了,但它不會返回沒有對話的用戶。它應該返回null或<3 – DMH