gpt4 book ai didi

mysql - 在 SQL 中使用 group by 时如何返回多列

转载 作者:行者123 更新时间:2023-11-30 23:55:10 24 4
gpt4 key购买 nike

我想要最高薪水低于 50,000 的员工姓名及其薪水和部门。我有以下内容

SELECT department, MAX(salary) as Highest salary
FROM employees
GROUP BY department
HAVING MAX(salary) < 50000

如何获取要返回的员工姓名?

最佳答案

这么近...

SELECT department, name, MAX(salary) as Highest salary
FROM employees
GROUP BY department, name
HAVING MAX(salary) < 50000

评论更新后

SELECT name, department , salary
FROM employees e
JOIN
(
SELECT department as dept, MAX(salary) as HighestSalary
FROM employees
GROUP BY department
) MaxE ON e.department = MaxE.dept AND e.salary = MaxE.HighestSalary

关于mysql - 在 SQL 中使用 group by 时如何返回多列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6930583/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com