metafex
metafex

Reputation: 169

MySQL, sorting before a group by

If I issue a query like this:

select c1, c2, c3
from table
group by c1;

i get distinct results for c1, but how do i sort it (e.g. c2 descending) before the group by?

Upvotes: 2

Views: 4208

Answers (3)

Tahbaza
Tahbaza

Reputation: 9546

Your query won't work as is. If I understand what you want try something closer to this:

select c1,c2,c3 from table group by c1,c2,c3 order by c1,c2 desc

Upvotes: 0

Michael Pakhantsov
Michael Pakhantsov

Reputation: 25390

Your question is unclear, but if you need got higher value of c2 for each c1 you may use Max

select c1, Max(c2), Max(c3)
from table
group by c1

Upvotes: 1

Vitalii Fedorenko
Vitalii Fedorenko

Reputation: 114570

select c1, c2, c3 
from (select c1, c2, c3 from table order by c2 desc) t 
group by c1;

Upvotes: 10

Related Questions