CrazyProgrammer
CrazyProgrammer

Reputation: 544

How to get a value of another field having max in another field?

Following is my table description. I want to get the value in StatusName where weight having maximum value. I want to select only the StatusName from this table.

status_weight
+------------+-------------+
| Field      | Type        |
+------------+-------------+
| id         | int(5)      |
| weight     | int(5)      |
| StatusName | varchar(25) |
+------------+-------------+

Upvotes: 0

Views: 51

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269493

You can use order by and limit:

select sw.*
from status_weight
order by weight desc
limit 1;

Note this returns one row with the maximum value. If you want all possible duplicates, then you need a more complex query. Something like:

select sw.*
from status_weight
where weight = (select max(weight) from status_weight);

Upvotes: 5

Related Questions