简体   繁体   English

如何在 mysql 的 group by 子句中最后创建 select 记录?

[英]How to select last created record in a group by clause in mysql?

I am using mysql 8.0.23我正在使用mysql 8.0.23

I have three tables, chats , chat_users and chat_messages我有三个表, chatschat_userschat_messages

I want to select the chat_id, the last message (with maximum createdAt date for a particular group. Said in other words, the message order by created_at desc within the group), from_user_id values for all the chats where user with id 1 is a member.我想要 select chat_id,最后一条消息(具有特定组的最大 createdAt 日期。换句话说,组内 created_at desc 的消息顺序),来自 id 1 的用户是成员的所有聊天的 from_user_id 值.

The tables sql and DDLs is are like below表 sql 和 DDL 如下所示

create table chats
(
    id         int unsigned auto_increment primary key,
    created_at timestamp default CURRENT_TIMESTAMP not null
);

create table if not exists chat_users
(
    id      int unsigned auto_increment
        primary key,
    chat_id    int unsigned not null,
    user_id int unsigned not null,
    constraint chat_users_user_id_chat_id_unique
        unique (user_id, chat_id),
    constraint chat_users_chat_id_foreign
        foreign key (chat_id) references chats (id)
);

create index chat_users_chat_id_index
    on chat_users (chat_id);

create index chat_users_user_id_index
    on chat_users (user_id);


create table chat_messages
(
    id       int unsigned auto_increment primary key,
    chat_id  int unsigned                            not null,
    from_user_id int unsigned                            not null,
    content      varchar(500) collate utf8mb4_unicode_ci not null,
    created_at   timestamp default CURRENT_TIMESTAMP     not null    constraint chat_messages_chat_id_foreign
        foreign key (chat_id) references chats (id),
);

create index chat_messages_chat_id_index
    on chat_messages (chat_id);
    
create index chat_messages_from_user_id_index
    on chat_messages (from_user_id);

The query that I tried so far and is not working properly is到目前为止我尝试过但无法正常工作的查询是


SET @userId = 1;
select
       c.id as chat_id,
       content,
       chm.from_user_id
from chat_users
         inner join chats c on chat_users.chat_id = c.id
         inner join chat_messages chm on c.id = chm.chat_id
where chat_users.user_id = @userId
group by c.id
order by c.id desc, max(chm.created_at) desc

My query above does not return the content field from the last created message, although I am trying to order by max(chm.created_at) desc.我上面的查询没有返回最后创建的消息的content字段,尽管我试图按 max(chm.created_at) desc 排序。 This order by after group by clause is executed after the grouping I think and not within the items from the group.. I know that I can probably select in the select statement the max date but I want to select last content value within the group not select max(ch.created_at) as last_created_at_msg_within_group这个 order by after group by 子句是在我认为的分组之后执行的,而不是在组中的项目中执行的。我知道我可以在 select 声明最大日期中的 select,但我想要 select 组中的最后一个内容值不是select max(ch.created_at) 作为 last_created_at_msg_within_group

I don't know how to select the content field from the item that has the highest chm.created_at from within the group that I do by grouping with c.id我不知道如何通过与 c.id 分组来 select 组中具有最高 chm.created_at 的项目的content字段

Example test data示例测试数据

chats

1 2021-07-23 20:51:01
2 2021-07-23 20:51:01
3 2021-07-23 20:51:01

chats_users

1 1 1
2 1 2
3 2 1 
4 2 2
5 3 1
6 3 2

chat_messages

1 1 1 lastmsg 2021-07-28 21:50:31
1 1 2 themsg  2021-07-23 20:51:01

The logic in this case should return这种情况下的逻辑应该返回

chat_id  content   from_user_id
1        lastmsg   1

PS: Before posting here I did my homework and studied similar questions in the forum, but they were trying to get last inserted row from a group and were not like mine. PS:在这里发帖之前,我做了功课并研究了论坛中的类似问题,但他们试图从一组中获取最后插入的行,而不是像我的那样。

Here's what I came up with, for a solution for MySQL 8.0 with window functions:这是我想出的,用于 MySQL 8.0 和 window 函数的解决方案:

select * from (
  select
      c.id as chat_id,
      content,
      chm.from_user_id,
      chm.created_at,
      row_number() over (partition by c.id order by chm.created_at desc) as rownum
  from chat_users
      inner join chats c on chat_users.chat_id = c.id
      inner join chat_messages chm on c.id = chm.chat_id
  where chat_users.user_id = @userId
) as t
where rownum = 1;

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

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