我正在尝试从精选计数中获取最小值。

SELECT selectSumOfToyCount.kidsFname, MIN(selectSumOfToyCount.sumToyCount)
(SELECT kidFName,  SUM(kids_to_toys.toyCount) sumToyCount
    FROM kids
        NATURAL JOIN kids_to_toys
        NATURAL JOIN toys
    GROUP BY kidFName
) selectSumOfToyCount
GROUP BY selectSumOfToyCount.kidsFname


()中的select语句可以工作,但是它总是使我从outside语句中出错。
我假设我的别名正确吗?

selectSumOfToyCount用于内部()所选语句,
sumToyCount作为总和。

最佳答案

只需按desc排序并获取第一行。

select * from (
 SELECT kidFName,  SUM(kids_to_toys.toyCount) sumToyCount
 FROM kids
    NATURAL JOIN kids_to_toys
    NATURAL JOIN toys
GROUP BY kidFName
)  order by sumToyCount desc limit 0,1

09-11 02:19