I have a simple table bc
b c
2 5
2 6
I'm trying to get line with biggest c
for each b
, but I'm getting c=5.
How can I do it in right way?
My select:
SET sql_mode = ''; SELECT * FROM `bc`
group by `bc`.b
ORDER BY `bc`.`c` desc
I have a simple table bc
b c
2 5
2 6
I'm trying to get line with biggest c
for each b
, but I'm getting c=5.
How can I do it in right way?
My select:
SET sql_mode = ''; SELECT * FROM `bc`
group by `bc`.b
ORDER BY `bc`.`c` desc
You can use aggregation:
select b, max(c)
from bc
group by b;
If you have more columns, you can use a correlated subquery in the where
clause;
select bc.*
from bc
where bc.c = (select max(bc2.c) from bc bc2 where bc2.b = bc.b);
use max
select b ,max(c) from table group by b