分组求和排序问题

有一个用户资金流水表(如上的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;

你可能感兴趣的:(分组求和排序问题)