分组求和排序问题

有一个用户资金流水表(如上的sql代码),找出流水金额最多的前10个用户:

create table tb_user_finance (

  id bigint primary key auto_increment,

  uid bigint not null default 0 comment '用户id',

  money decimal(10, 2) not null default 0.00 comment '资金流水',

  type tinyint not null default 0 comment '1: 转账, 10: 提现, 20: 充值',

  created_at timestamp not null default current_timestamp,

  updated_at timestamp not null default current_timestamp on update current_timestamp,

  key ix_uid (uid)

) engine = innodb default charset=utf8 comment '用户资金流水表';

insert into tb_user_finance (uid, money, type) values(10, 20, 1);

insert into tb_user_finance (uid, money, type) values(10, 20, 1);

insert into tb_user_finance (uid, money, type) values(10, 20, 1);

insert into tb_user_finance (uid, money, type) values(10, 200, 1);

insert into tb_user_finance (uid, money, type) values(20, 10, 10);

insert into tb_user_finance (uid, money, type) values(30, 20, 20);

insert into tb_user_finance (uid, money, type) values(30, 10, 20);

insert into tb_user_finance (uid, money, type) values(31, 10, 20);

insert into tb_user_finance (uid, money, type) values(32, 20, 20);

insert into tb_user_finance (uid, money, type) values(33, 45, 20);

insert into tb_user_finance (uid, money, type) values(34, 100, 20);

insert into tb_user_finance (uid, money, type) values(35, 1000, 20);

insert into tb_user_finance (uid, money, type) values(36, 1090, 20);

有一个用户资金流水表(如上的sql代码),找出流水金额最多的前10个用户:

select uid, sum(money) as total from tb_user_finance group by uid order by total desc limit 10;

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容