Joss D.
Joss D.

Reputation: 77

Select id, max(value) - Can't get right id

I have a table with some columns where I want to select 'id' and the highest 'value'.

When I do :

SELECT id, max(value) AS highest_value FROM table WHERE id_column='2' GROUP BY id

I get the right values of id, but I get multiple rows (normal behavior).

So as I want only the id of the highest value I do the standard request which is :

SELECT id, max(value) AS highest_value FROM table WHERE id_column='2' 

Then I have another id for the same highest_value... Can't understand why.

Upvotes: 0

Views: 495

Answers (1)

Uueerdo
Uueerdo

Reputation: 15941

...
ORDER BY highest_value DESC
LIMIT 1

If id is unique, you don't even need the max() and GROUP BY.

SELECT id
FROM table
WHERE id_column = '2'
ORDER BY value DESC
LIMIT 1
;

Upvotes: 1

Related Questions