Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL MIN/MAX all row

I have table Races with the rows ID, Name and TotalCP . I SELECT MIN( TotalCP ) FROM Races, but then I want to select the entire row which have the minimum value. How I can make that, in a single query ?

like image 280
Flavius Avatar asked Dec 17 '25 23:12

Flavius


1 Answers

The general form for getting a whole row from an aggregated value is:

SELECT *
FROM Races
WHERE TotalCP = (SELECT MIN(TotalCP) FROM Races)

or

SELECT r.*
FROM
(
    SELECT MIN(TotalCP) t
    FROM Races
) m
INNER JOIN Races r ON m.t = r.TotalCP

However, in this case, since you're using MIN, you can just sort and take the first row:

SELECT *
FROM Races
ORDER BY TotalCP
LIMIT 1
like image 155
lc. Avatar answered Dec 19 '25 14:12

lc.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!