简体   繁体   English

从 MySQL 中选择数据不相同的行

[英]Select rows from MySQL where data is not the same

So i have a chat system and a topbar with notifications What i want to do is load the last message from the last X persons the user has chatted So i dont want to spam the notifications with X number of messages from the same person but i want to load the very LAST message from X number of persons所以我有一个聊天系统和一个带有通知的顶栏我想要做的是加载用户聊天的最后 X 个人的最后一条消息 所以我不想用来自同一个人的 X 条消息向通知发送垃圾邮件,但我想要加载来自 X 个人的最后一条消息

So here is my problem (example): Imagine if one person sent the user 8 messages and i limit the message query to 8 results.所以这是我的问题(示例):想象一下,如果一个人向用户发送了 8 条消息,而我将消息查询限制为 8 个结果。 The query will return 8 messages from the same person but i've made a PHP filter that will echo only 1 message from one person.该查询将返回来自同一个人的 8 条消息,但我制作了一个 PHP 过滤器,该过滤器将仅回显来自一个人的 1 条消息。 So basically this will only echo 1 message from 1 person but wont echo any other messages from any other person because ive limited the result query to 8 and this user already spammed 8 messages所以基本上这只会回显来自 1 个人的 1 条消息,但不会回显来自任何其他人的任何其他消息,因为我将结果查询限制为 8 并且该用户已经发送了 8 条消息

This is my PHP filter (but i want to do this in SQL)这是我的 PHP 过滤器(但我想在 SQL 中执行此操作)

foreach ($sideMess as $smes) {
    if (!isset($showedList[$smes['id']])) { 
        echo '<div class="mes_wrap" id="ppl_', $smes['id'] ,'">
            <img src="', $smes['avatar'] ,'">
            <h4>', $smes['firstname'] ,' ', $smes['lastname'] ,'</h4>
            </div>';

        $showedList[$smes['id']] = true;
        $cS ++;
    }
}

As per your specification, it seems that you want list of last message from multiple users, ie, user-wise last message.根据您的规范,您似乎想要来自多个用户的最后一条消息列表,即用户明智的最后一条消息。

select * from
(select * from
messages
order by user_id,message_time desc) temp
group by user_id;

Okay, its solved!好的,解决了! I used GROUP BY sender_id before ORDER BY Thanks for your help!我在 ORDER BY 之前使用了 GROUP BY sender_id 感谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM