MySQL 获取最后一条消息 (VarChar)
MySQL get last Message (VarChar)
我正在尝试从某个线程获取最后一条消息,但似乎无法正确查询。
我似乎没有收到某个主题的最后一条消息。
简介
我使用了 3 个表 -- message,message_users,message_threads
所有消息都属于某个主题(有点像论坛和帖子的运作方式?)
用户可以将多个用户添加到某个线程,这样多个用户就可以在同一个线程中互相发消息了。
数据库结构
messages table
id | t_id | u_id | message | time
--------+---------+--------+-----------+--------
1 | 1 | 1 | hello! | timestamp
2 | 1 | 2 | hi! | timestamp
3 | 2 | 1 | testing | timestamp
message_threads
id | author | subject
------+---------+-----------
1 | 1 | Subject 1
2 | 3 | Subject 2
3 | 2 | Subject 3
message_users
id | t_id | u_id | read | nonotice
------+---------+--------+--------+----------
1 | 1 | 1 | 0 | 0
2 | 1 | 2 | 0 | 0
3 | 2 | 1 | 1 | 0
查询
这是我正在使用的查询,它几乎可以工作,只是它没有获得最后的 ID,也没有获得最后的消息,有什么想法吗?
(是的,我已经查看并搜索过有关此主题的先前问题,但无法以某种方式根据答案修复查询)
SELECT
m2.message,
mt.id AS thread_id,
max(m.id) AS message_id,
max(m.time) AS time,
mt.subject
FROM message_users AS mu
LEFT JOIN message_threads AS mt ON mt.id = mu.t_id
LEFT JOIN message AS m ON m.t_id = mt.id
INNER JOIN (
select message as messages_text
from message group by t_id
) AS m2 ON m2.id = m.id
WHERE mu.u_id = 1 AND mu.read = 0 AND mu.nonotice = 0
GROUP BY mt.id
ORDER BY m.id DESC
SELECT
m.t_id,
m.message,
m.id AS max_message_id,
m.ts AS max_message_ts,
t.subject
FROM messages AS m
JOIN message_threads AS t ON m.t_id = t.id
JOIN (
SELECT t_id, max(id) max_id FROM messages GROUP BY 1
) AS m2 ON m.id = m2.max_id
JOIN message_users AS u ON u.t_id = t.id AND u.u_id = 1 AND u.`read` = 0 AND u.nonotice = 0
如果上面 SQL 变慢,您可以执行以下任一操作:
1 - 将 where 添加到内部联接 SQL 中以使结果更小,即:
SELECT
m.t_id,
m.message,
m.id AS max_message_id,
m.ts AS max_message_ts,
m2.subject
FROM messages AS m
JOIN (
SELECT t.id, max(m.id) AS max_id, t.subject
FROM messages AS m
JOIN message_threads AS t ON m.t_id = t.id
JOIN message_users AS u ON u.t_id = t.id
AND u.u_id = 1 AND u.`read` = 0 AND u.nonotice = 0
GROUP BY 1
) AS m2 ON m.id = m2.max_id
2 - 重新设计 message_thread 添加计算列:(last_message_id, last_updated) 等
PS - 尽量避免使用 MySQL 关键字作为您的列名称
我正在尝试从某个线程获取最后一条消息,但似乎无法正确查询。 我似乎没有收到某个主题的最后一条消息。
简介
我使用了 3 个表 -- message,message_users,message_threads
所有消息都属于某个主题(有点像论坛和帖子的运作方式?) 用户可以将多个用户添加到某个线程,这样多个用户就可以在同一个线程中互相发消息了。
数据库结构
messages table
id | t_id | u_id | message | time
--------+---------+--------+-----------+--------
1 | 1 | 1 | hello! | timestamp
2 | 1 | 2 | hi! | timestamp
3 | 2 | 1 | testing | timestamp
message_threads
id | author | subject
------+---------+-----------
1 | 1 | Subject 1
2 | 3 | Subject 2
3 | 2 | Subject 3
message_users
id | t_id | u_id | read | nonotice
------+---------+--------+--------+----------
1 | 1 | 1 | 0 | 0
2 | 1 | 2 | 0 | 0
3 | 2 | 1 | 1 | 0
查询
这是我正在使用的查询,它几乎可以工作,只是它没有获得最后的 ID,也没有获得最后的消息,有什么想法吗? (是的,我已经查看并搜索过有关此主题的先前问题,但无法以某种方式根据答案修复查询)
SELECT
m2.message,
mt.id AS thread_id,
max(m.id) AS message_id,
max(m.time) AS time,
mt.subject
FROM message_users AS mu
LEFT JOIN message_threads AS mt ON mt.id = mu.t_id
LEFT JOIN message AS m ON m.t_id = mt.id
INNER JOIN (
select message as messages_text
from message group by t_id
) AS m2 ON m2.id = m.id
WHERE mu.u_id = 1 AND mu.read = 0 AND mu.nonotice = 0
GROUP BY mt.id
ORDER BY m.id DESC
SELECT
m.t_id,
m.message,
m.id AS max_message_id,
m.ts AS max_message_ts,
t.subject
FROM messages AS m
JOIN message_threads AS t ON m.t_id = t.id
JOIN (
SELECT t_id, max(id) max_id FROM messages GROUP BY 1
) AS m2 ON m.id = m2.max_id
JOIN message_users AS u ON u.t_id = t.id AND u.u_id = 1 AND u.`read` = 0 AND u.nonotice = 0
如果上面 SQL 变慢,您可以执行以下任一操作:
1 - 将 where 添加到内部联接 SQL 中以使结果更小,即:
SELECT
m.t_id,
m.message,
m.id AS max_message_id,
m.ts AS max_message_ts,
m2.subject
FROM messages AS m
JOIN (
SELECT t.id, max(m.id) AS max_id, t.subject
FROM messages AS m
JOIN message_threads AS t ON m.t_id = t.id
JOIN message_users AS u ON u.t_id = t.id
AND u.u_id = 1 AND u.`read` = 0 AND u.nonotice = 0
GROUP BY 1
) AS m2 ON m.id = m2.max_id
2 - 重新设计 message_thread 添加计算列:(last_message_id, last_updated) 等
PS - 尽量避免使用 MySQL 关键字作为您的列名称