sql根据用户对话显示未读消息

时间:2017-07-21 08:20:29

标签: mysql sql

我有以下聊天表结构:

+----+---------+-------+--------+------------+
| id | from_id | to_id | pet_id | created_at |
+----+---------+-------+--------+------------+
| 72 |      41 |    39 |    136 |   15000621 |
| 73 |      41 |    39 |    156 |    1111111 |
| 74 |      41 |    43 |    158 |     222222 |
+----+---------+-------+--------+------------+

消息表:

+------+--------------+--------------+---------+------------+---------+-------------+---+
|  id  | chat_room_id | user_role_id | message | created_at |  read   |             |   |
+------+--------------+--------------+---------+------------+---------+-------------+---+
| 354  |          72  |           41 |  hello  |  1500621660 | 1 |
| 355  |          72  |           39 |  geiaaa | 1500621697  | 0 |
| 356  |          72  |           39 |  again  | 1500621702  | 0 |
| 357  |          73  |           41 |  fgh    | 1500621725  | 1 |
| 358  |          73  |           39 |  fhh    | 1500621736  | 0 |
| 359  |          73  |           39 |  ert    | 1500621739  | 0 |
| 360  |          74  |           41 |  ER     | 1500621780  | 0 |
+------+--------------+--------------+---------+------------+---------+-------------+---+

用户表:

 +----+
 | id | 
 +----+
 | 44 |

和UserRole表:

 +-----+---------+------+
 | id  | user_id | role |
 +-----+---------+------+ 
 | 41  | 44      | 1    |
 | 44  | 44      | 2    |
 | 45  | 44      | 3    |
 +-----+---------+------+     

我想收听特定用户聊天的最后一条消息,以及每次聊天的未读消息总数。我试过的查询解决了问题的第一部分:

(SELECT ChatRoom.id, message FROM DogVIP.ChatRoom 
     left join Message on ChatRoom.id = Message.chat_room_id
     left join UserRole on ChatRoom.from_id = UserRole.id
where 
    to_id in (select UserRole.id from UserRole left join User on UserRole.user_id = User.id where UserRole.user_id = 44) and
    Message.id in (SELECT MAX(id) FROM Message GROUP BY Message.chat_room_id)
)
union distinct
(SELECT ChatRoom.id, message FROM DogVIP.ChatRoom 
     left join Message on ChatRoom.id = Message.chat_room_id
     left join UserRole on ChatRoom.to_id = UserRole.id
where from_id in (select UserRole.id from UserRole left join User on UserRole.user_id = User.id where UserRole.user_id = 44) and
Message.id in (SELECT MAX(id) FROM Message GROUP BY Message.chat_room_id));

输出结果为:

+----+---------+
| id | message |
+----+---------+
| 72 | again   |
| 73 | ert     |
| 74 | ER      |
+----+---------+

我想要的输出是:

+----+---------+--------+
| id | message | total  |
+----+---------+--------+
| 72 | again   | 2      |
| 73 | ert     | 2      |
| 74 | ER      | 1      |
+----+---------+--------+

1 个答案:

答案 0 :(得分:1)

这将列出聊天室ID,最后一条消息和所有聊天室的总数。

现在,当你得到一个想法时,你可以添加所需的WHERE条件和JOINs

SELECT
  tlatest.chat_room_id,
  tlastmessage.message,
  COUNT(tunread.id) as total
FROM
(
SELECT
  chat_room_id,
  MAX(created_at) as last_created_at
FROM
  Message
GROUP BY
  chat_room_id
) tlatest
LEFT JOIN 
  Message tm ON tm.chat_room_id = tlatest.chat_room_id AND tm.created_at = tlatest.last_created_at
LEFT JOIN 
  Message tunread ON tunread.chat_room_id = tlatest.chat_room_id AND tunread.read = 0
GROUP BY tlatest.chat_room_id

在你的情况下我猜它会是

SELECT
  tlatest.chat_room_id,
  tlastmessage.message,
  COUNT(tunread.id) as total
FROM
(
SELECT
  chat_room_id,
  MAX(created_at) as last_created_at
FROM
  Message
JOIN 
  Chat ON Chat.id = Message.chat_room_id
WHERE
  Chat.from_id in (select UserRole.id from UserRole left join User on UserRole.user_id = User.id where UserRole.user_id = 44) OR
  Chat.to_id in (select UserRole.id from UserRole left join User on UserRole.user_id = User.id where UserRole.user_id = 44)
GROUP BY
  chat_room_id
) tlatest
LEFT JOIN 
  Message tm ON tm.chat_room_id = tlatest.chat_room_id AND tm.created_at = tlatest.last_created_at
LEFT JOIN 
  Message tunread ON tunread.chat_room_id = tlatest.chat_room_id AND tunread.read = 0
GROUP BY tlatest.chat_room_id