Answer the question
In order to leave comments, you need to log in
SELECT, IN, LIMIT. How to make a request?
Good afternoon.
Table in the database
field_a | field_b |
one | one |
one | 2 |
one | 3 |
one | 4 |
2 | one |
2 | 2 |
2 | 3 |
2 | 4 |
3 | one |
3 | 2 |
3 | 3 |
3 | 4 |
SELECT * FROM `table` WHERE `field_a` IN (1, 2) ORDER BY `field_b` DESC
will return all rows where `field_a` is 1 or 2, sorted by `field_b`. field_a | field_b |
one | 4 |
one | 3 |
2 | 4 |
2 | 3 |
Answer the question
In order to leave comments, you need to log in
This option is correct, but rather slow.
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question