我正在嘗試讓所有用戶少於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
定義_unanswered_。 – jarlh
未答覆的對話意味着:只有一封發件人發送給收件人但未收到收件人回覆的郵件的會話。 – DMH
首先,您需要一個返回那些未答覆的對話的子查詢。然後你加入並分組。 – jarlh