我有三张桌子,我需要不同的数据。可悲的是,我还需要能够提取最新的一行。
以下是我的表格:
消息:我只是将消息内容存储在一个表中,因为一个文本可以发送给多个用户
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| message_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| content | varchar(255) | NO | | 0 | |
+------------+------------------+------+-----+---------+----------------+
对话:此表仅反映两个用户之间的单个对话。
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| partner_id | int(10) unsigned | NO | MUL | NULL | |
| conversation_id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| expedition_id | int(11) | NO | | NULL | |
| active | tinyint(4) | NO | | 1 | |
+-----------------+------------------+------+-----+---------+----------------+
conversation_messages :此表存储有关所交换实际消息的信息。
+-----------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+-------+
| message_id | int(11) unsigned | NO | PRI | NULL | |
| receiver_id | int(11) unsigned | NO | PRI | NULL | |
| conversation_id | int(11) unsigned | NO | MUL | NULL | |
| status | tinyint(4) | NO | | NULL | |
| timestamp | datetime | YES | | NULL | |
+-----------------+------------------+------+-----+---------+-------+
我现在想要做的是选择每个对话中的最新消息,并从此消息中获取内容。 (听起来很简单,但没有找到简单的解决方案)。我尝试的是以下内容:
SELECT max(c_m.message_id), m.content, c_m.`status`
FROM expedition_conversations e_c, conversation_messages c_m
INNER JOIN messages m ON m.message_id = c_m.message_id
WHERE e_c.expedition_id = 1 AND (c_m.conversation_id = e_c.conversation_id)
GROUP BY c_m.conversation_id;
遗憾的是,由于GROUP BY
内部似乎大部分时间都在选择第一个插入的行,因此从content
表中选择的messages
是错误的,而message_id
已选中来自conversation_messages
是正确的。
知道如何在一个查询中执行此操作吗?如果您有任何改变表结构的建议,我也很感激。
提前致谢。
答案 0 :(得分:1)
这是可能的,因为您使用AUTO_INCREMENT
表示最高ID属于最新消息:
SELECT
messages.*,
FROM
conversations
INNER JOIN (
SELECT conversation_id, MAX(message_id) AS maxmsgid
FROM conversation_messages
GROUP BY conversation_id
) AS latest ON latest.conversation_id=conversations.id
INNER JOIN messages
ON messages.message_id=latest.maxmsgid
WHERE
1=1 -- whatever you want or need!
由于此查询的速度非常慢,您可能需要考虑以下几个选项:
ON AFTER INSERT
上messages
触发更新conversation_messages
上包含最新消息ID的字段答案 1 :(得分:1)
您可能想尝试此版本:
SELECT c_m.message_id, m.content, c_m.`status`
FROM expedition_conversations e_c join
conversation_messages c_m
ON c_m.conversation_id = e_c.conversation_id INNER JOIN
messages m
ON m.message_id = c_m.message_id
WHERE e_c.expedition_id = 1 AND
NOT EXISTS (SELECT 1
FROM conversation_messages cm2
WHERE cm2.conversation_id = c_m.conversation_id AND
cm2.timestamp > c_m.timestamp
)
为了提高性能,您需要conversation_messages(conversation_id, timestamp)
上的索引。
答案 2 :(得分:0)
尝试这个小技巧:
SELECT c_m.message_id, m.content, c_m.status
FROM expedition_conversations e_c
JOIN (select * from (
select * from conversation_messages
order by message_id desc) x
group by conversation_id) c_m ON c_m.conversation_id = e_c.conversation_id
INNER JOIN messages m ON m.message_id = c_m.message_id
WHERE e_c.expedition_id = 1
这将适用于您的版本,如果mysql - 5.6.19 - 并且应该超出其他方法。