SQL从/向某些用户获取最后的消息

时间:2013-06-17 09:35:26

标签: mysql sql join

我试图从表中进行SQL查询,   - 从用户36作为发件人或收件人的所有用户对中获取最后一封邮件,并将其与users表连接以获取名称。我设法创造了这样的东西,但仍然想问是否有更简单的|有效解决方案 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;

感谢。 UPD。样本消息表中的数据:

的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

1 个答案:

答案 0 :(得分:8)

我正在使用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小提琴: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

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

更新了SQL小提琴:http://sqlfiddle.com/#!3/1f07a/2