MySQL的MIN / MAX所有的行(MySQL MIN/MAX all row)

2019-08-02 11:58发布

我有表Races与行的IDNameTotalCP 。 我选择MIN( TotalCP )FROM Races ,但后来我想选择具有最小值的整行。 我怎样才能让,在一个单一的查询?

Answer 1:

从聚合值获得一整排的一般形式是:

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

要么

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

然而,在这种情况下,由于您使用MIN ,你可以进行排序,并采取的第一行:

SELECT *
FROM Races
ORDER BY TotalCP
LIMIT 1


Answer 2:

子查询是你的赌注,

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


Answer 3:

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


文章来源: MySQL MIN/MAX all row