Say, I have a table:
id ext_id param
1 5 0
2 5 1
3 6 0
4 6 0
5 7 1
6 7 1
respectively.
I wanna have a result like:
2 5 1
3 6 0
5 7 1
So that MySQL groups by ext_id and shows an element, that has maximum param value. If all are equal – I need any.
I tried something like:
SELECT * FROM t
GROUP BY ext_id HAVING param = MAX(param)
but it did not do the trick ((
First you have to find the maximum
paramfor every ext_id, using a group by query. Then you have to join the result of this query again on t, whereext_idmatches, andparamismax_param:Since there could be more than one row with the same maximum
param, I’m grouping again byext_id, MySql allows you to do so, so it will get just one row for eachext_id.