0

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
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
Lamantinoss
  • 159
  • 1
  • 8
  • Thank you for showing other question. I've been searching a lot, and couldn't find it, there were others wrong answers on the same questions, I needed this link. – Lamantinoss Sep 09 '19 at 17:51

2 Answers2

0

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);
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

use max

   select b ,max(c) from table group by b
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
  • I need to select LINE with biggest c, not just max c value. For example, if I'll add new field d, I'll not select line with d from the lime with biggest c, it's not what i need – Lamantinoss Sep 09 '19 at 17:18