从消息线程中获取嵌套的未读消息

时间:2012-01-19 03:08:07

标签: mysql sql

我有两个表,message_threadsmessages。返回结果集message_threads时,我在两个表上执行JOIN,以查看该线程中发件人(发送者)的任何消息是否为未读。

以下是我的SQL ...

SELECT mt.id AS thread_id, m.id AS message_id,
m.is_read, m.from_type, mt.company_id
FROM message_threads AS mt
LEFT JOIN messages AS m
ON m.thread_id = mt.id;

...来自给定查询的完整结果...

+-----------+------------+---------+-----------+------------+
| thread_id | message_id | is_read | from_type | company_id |
+-----------+------------+---------+-----------+------------+
|         1 |          1 | N       | company   |          1 |
|         1 |          9 | N       | company   |          1 |
|         1 |         19 | N       | company   |          1 |
|         2 |          2 | Y       | coder     |          1 |
|         2 |          3 | N       | company   |          1 |
|         2 |          6 | N       | company   |          1 |
|         2 |          8 | N       | company   |          1 |
|         3 |          4 | N       | company   |          1 |
|         6 |         13 | N       | company   |          1 |
|         6 |         14 | N       | coder     |          1 |
|         6 |         15 | N       | company   |          1 |
|         8 |         20 | N       | company   |          1 |
|         8 |         21 | N       | coder     |          1 |
|         4 |          5 | N       | company   |          2 |
|         4 |          7 | N       | company   |          2 |
|         4 |         22 | N       | coder     |          2 |
|         5 |         10 | N       | company   |          8 |
|         5 |         11 | N       | coder     |          8 |
|         5 |         12 | N       | company   |          8 |
|         7 |         16 | N       | company   |         18 |
|         7 |         17 | N       | coder     |         18 |
|         7 |         18 | N       | company   |         18 |
+-----------+------------+---------+-----------+------------+

...和所需的结果集:

+-----------+------------+---------+-----------+------------+
| thread_id | message_id | is_read | from_type | company_id |
+-----------+------------+---------+-----------+------------+
|         1 |         19 | N       | company   |          1 |
|         2 |          2 | Y       | coder     |          1 |
|         3 |          4 | N       | company   |          1 |
|         6 |         14 | N       | coder     |          1 |
|         8 |         21 | N       | coder     |          1 |
|         4 |         22 | N       | coder     |          2 |
|         5 |         11 | N       | coder     |          8 |
|         7 |         17 | N       | coder     |         18 |
+-----------+------------+---------+-----------+------------+

如何执行此查询?我已经尝试了GROUP BYDISTINCT,但我也没有做到我想要的。我也不能使用WHERE子句来过滤我的数据,因为无论is_read标志如何,我都需要所有线程。

谢谢!

1 个答案:

答案 0 :(得分:2)

有很多方法可以做到这一点。如果您希望查看线程中未读消息的数量,您可能希望加入内部查询。

示例:

SELECT 
mt.id AS thread_id, 
mt.company_id,
CASE WHEN m_c.m_unread IS NOT NULL THEN m_c.m_unread ELSE 0 END AS unread
FROM message_threads AS mt
LEFT JOIN 
( 
  SELECT
  thread_id,
  COUNT(*) AS m_unread
  FROM messages
  WHERE
  is_read ='N'
  GROUP BY 1
)m_c
ON mt.thread_id = m_c.thread_id