mysql - Get best students from each class limit 3 results -


i need 2 best students each class. try

 select t1.nota,t2.sala,t3.serie aluno t1 left join sala t2 on t1.sala = t2.id left join serie t3 on t1.serie = t3.id  order t1.serie,t1.sala,t1.nota  limit t1.nota 2 ?? 

actual query result in:

  nota    sala    serie 9       1-102   1 ano 8.9     1-102   1 ano 9.1     1-102   1 ano 8.2     1-201   2 ano 9       1-201   2 ano 7.8     1-201   2 ano 9       1-303   3 ano 10      1-303   3 ano 8.7     1-303   3 ano 10      1-102   1 ano 

i need from

  nota    sala    serie 10      1-102   1 ano 9.1     1-102   1 ano 8.2     1-201   2 ano 9       1-201   2 ano 9       1-303   3 ano 10      1-303   3 ano 

------

 create table if not exists `aluno` (   `id` int(11) not null auto_increment,   `serie` int(11) not null,   `sala` int(11) not null,   `nota` int(11) not null,   primary key (`id`) ) engine=innodb  default charset=latin1 auto_increment=11 ;    insert `aluno` (`id`, `serie`, `sala`, `nota`) values (1, 1, 2, 9), (2, 1, 2, 10), (3, 2, 2, 8), (4, 2, 2, 9), (5, 1, 2, 10), (6, 5, 2, 10), (7, 3, 3, 8), (8, 3, 3, 9), (9, 3, 3, 10), (10, 3, 3, 10);    create table if not exists `sala` (   `id` int(11) not null auto_increment,   `serie` varchar(50) not null,   `sala` varchar(50) not null,   primary key (`id`) ) engine=innodb  default charset=latin1 auto_increment=9 ;    insert `sala` (`id`, `serie`, `sala`) values (1, '1', '1-101'), (2, '1', '1-102'), (3, '1', '1-103'), (4, '1', '1-104'), (5, '2', '2-101'), (6, '2', '2-102'), (7, '2', '2-103'), (8, '2', '2-104');    create table if not exists `serie` (   `id` int(11) not null auto_increment,   `serie` varchar(50) not null,   primary key (`id`) ) engine=innodb  default charset=latin1 auto_increment=3 ;    insert `serie` (`id`, `serie`) values (1, '1 ano'), (2, '2 ano'); 

@strawberry , @sgedds attemption. find solution with

set @num := 0, @salas := ''; select t1.id, t3.serie,t2.sala, t1.nome,t1.nota,t1.sala,   @num := if(@salas = t1.sala, @num + 1, 1) row_number,   @cols := t1.sala dummy aluno t1 left join sala t2 on t1.sala = t2.id left join serie t3 on t1.serie = t3.id t2.id in(10,11,12) , t1.nota >='8' having dummy <=11 order t3.serie asc,t2.sala asc, t1.nota desc,nome asc  

Comments

Popular posts from this blog

how to proxy from https to http with lighttpd -

android - Automated my builds -

python - Flask migration error -