我怎样才能到达sumofsalary结果的顶部查询

ndh0cuux  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(241)
SELECT 
FORMAT(SUM(e.salary),0) as sumOfSalary, 
d.departName 
FROM
employee e 
JOIN depart d on (d.id = e.depart) 
GROUP BY d.departName
ORDER BY sumOfSalary desc

如何返回 sumOfSalary 不使用限制1?我想用 MAX()SUM(e.salary) 但这无助于我得到一个错误#1111-组函数的无效使用

hpxqektj

hpxqektj1#

使用limit仅返回查询的顶部结果:

SELECT 
    FORMAT(SUM(e.salary),0) as sumOfSalary, 
    d.departName 
FROM
    employee e 
JOIN depart d 
    on (d.id = e.depart) 
GROUP BY d.departName
ORDER BY sumOfSalary desc
LIMIT 1
slmsl1lt

slmsl1lt2#

当您使用格式时,结果不是按数字值排序的,而是按字母顺序排列的,所以“600”按降序排在“3000”之前

select departName, format(max(sumOfSalary),0) from 
 (SELECT 
    SUM(e.salary) as sumOfSalary, 
    d.departName 
    FROM
    employee e 
    JOIN depart d on (d.id = e.depart) 
    GROUP BY d.departName
    ORDER BY SUM(e.salary) desc limit 1) as nested
vh0rcniy

vh0rcniy3#

如果你只需要所有部门中的最高工资(我希望我正确地回答了你的问题!),然后使用嵌套查询:

SELECT FORMAT(MAX(sumOfSalary),0) FROM (
SELECT 
SUM(e.salary) as sumOfSalary, d.departName 
FROM employee e 
JOIN depart d on d.id = e.depart)
GROUP BY d.departName
) AS ss

相关问题