gpt4 book ai didi

mysql - SQL从/到特定用户获取最后消息

转载 作者:可可西里 更新时间:2023-11-01 07:20:31 25 4
gpt4 key购买 nike

我正在尝试从表中进行 SQL 查询, - 从所有以用户 36 作为发件人或收件人的用户对获取最后一条消息,并将它们与用户表连接起来以获取名称。我已经设法创建了这样的东西,但仍然想问一下是否有更简单的 |有效的解决方案。Mysql 版本 - 5.5.31

    table: messages
fields:
sender_user_id, recipient_user_id, date, text

查询:

SELECT 
*
FROM
(SELECT
(CASE sender_user_id > recipient_user_id
WHEN true THEN CONCAT(recipient_user_id, '|', sender_user_id)
WHEN false THEN CONCAT(sender_user_id, '|', recipient_user_id)
END) as hash,
sender_user_id,
recipient_user_id,
date,
text,
u.first_name
FROM
fed_messages
LEFT JOIN fed_users as u ON ((fed_messages.sender_user_id = 36 AND fed_messages.recipient_user_id = u.id) OR (fed_messages.recipient_user_id = 36 AND fed_messages.sender_user_id = u.id))
WHERE
sender_user_id = 36 OR recipient_user_id = 36
ORDER BY date DESC) as main
GROUP BY hash;

谢谢。更新。示例消息表中的数据:

mysql> SELECT id, sender_user_id, recipient_user_id, text, date FROM federation.fed_messages WHERE id > 257;

-----+----------------+-------------------+-----------------+---------------------+
| id | sender_user_id | recipient_user_id | text | date |
+-----+----------------+-------------------+-----------------+---------------------+
| 258 | 40 | 36 | and one more | 2013-06-06 10:57:17 |
| 259 | 36 | 38 | another message | 2013-06-06 11:03:49 |
| 260 | 38 | 36 | some mes | 2013-06-06 12:29:33 |
| 261 | 38 | 36 | message | 2013-06-06 12:29:53 |
| 262 | 36 | 38 | message | 2013-06-06 12:47:26 |
| 263 | 36 | 40 | some message | 2013-06-10 16:22:46 |

结果应该是 ids - 262, 263

最佳答案

我用的是SQL Server 2008,你不说你用的是哪个数据库。

从您提供的信息来看,您的查询对于您需要的输出来说似乎过于复杂。下面是一个简单的查询,用于获取涉及用户 36 的所有消息:

SELECT  
sender.msg_user_name AS sender_user_name
,recipient.msg_user_name AS recipient_user_name
,msg_date
,msg_text

FROM
dbo.Fed_Messages

INNER JOIN dbo.Fed_User AS sender
ON sender.msg_user_id = sender_user_id

INNER JOIN dbo.Fed_User AS recipient
ON recipient.msg_user_id = recipient_user_id

WHERE
sender_user_id = 36
OR recipient_user_id = 36

ORDER BY
msg_date DESC

我不得不更改一些字段名称,因为在 SQL Server 中,您选择的一些名称是保留字。

SQL fiddle :http://sqlfiddle.com/#!3/b8e88/1

编辑:现在您已经添加了更多信息,并显示消息表上有一个 id 字段,您可以使用类似这样的东西(注意:我有 SQL Server,所以您可能需要更改查询对于 MySQL):

SELECT  sender.msg_user_name AS sender_user_name
,recipient.msg_user_name AS recipient_user_name
,msg_date
,msg_text
FROM dbo.Fed_Messages
INNER JOIN dbo.Fed_User AS sender ON sender.msg_user_id = sender_user_id
INNER JOIN dbo.Fed_User AS recipient ON recipient.msg_user_id = recipient_user_id
INNER JOIN ( SELECT MAX(id) AS most_recent_message_id
FROM dbo.Fed_Messages
GROUP BY CASE WHEN sender_user_id > recipient_user_id
THEN recipient_user_id
ELSE sender_user_id
END -- low_id
,CASE WHEN sender_user_id < recipient_user_id
THEN recipient_user_id
ELSE sender_user_id
END -- high_id
) T ON T.most_recent_message_id = dbo.Fed_Messages.id
WHERE sender_user_id = 36
OR recipient_user_id = 36
ORDER BY msg_date DESC

查询的 FROM 部分中的 SELECT 找到最近的消息(基于 id,我假设它是一个自动递增数字)为每对有序的发件人/收件人用户 ID。其结果将重新加入 Fed_Messages 表,以确保我们获得正确的发件人/收件人姓名。

更新的 SQL fiddle :http://sqlfiddle.com/#!3/1f07a/2

关于mysql - SQL从/到特定用户获取最后消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17144447/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com