MySQL MIN/MAX all row

ⅰ亾dé卋堺 提交于 2019-12-05 03:24:13

问题


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 ?


回答1:


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



回答2:


Subquery is your bet,

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



回答3:


Select * from Races
where TotalCP = SELECT MIN( TotalCP ) FROM Races


来源:https://stackoverflow.com/questions/13293778/mysql-min-max-all-row

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!