field_a | field_b |
1 | 1 |
1 | 2 |
1 | 3 |
1 | 4 |
2 | 1 |
2 | 2 |
2 | 3 |
2 | 4 |
3 | 1 |
3 | 2 |
3 | 3 |
3 | 4 |
SELECT * FROM `table` WHERE `field_a` IN (1, 2) ORDER BY `field_b` DESC
вернет все строки, в которых `field_a` равно 1 или 2, отсортированные по полю `field_b`.field_a | field_b |
1 | 4 |
1 | 3 |
2 | 4 |
2 | 3 |
SET @rank=0;
SET @current_field=0;
select a.field_a, a.field_b,
@rank:=if(@current_field = a.field_a,@rank+1,0) as rank,
@current_field:=a.field_a as current
FROM (select field_a, field_b from test2 where field_a IN (1,2)
ORDER BY field_a, field_b DESC ) as a having rank < 3;