gpt4 book ai didi

mysql - 使用外键关系的 SQL 查询

转载 作者:搜寻专家 更新时间:2023-10-30 22:14:28 25 4
gpt4 key购买 nike

我正在学习 MySQL,我正在尝试在查询中进行简单的算术运算,我正在尝试确定平均工资最高的部门并获取部门名称。但我不知道如何使用外键访问 ProfessorsTable 表。任何人都可以帮助我吗?谢谢!

这是我试过的:

 SELECT Department FROM ProfessorsTable WHERE max(AVG(Salary));

下面是我的两个表:

 CREATE TABLE DepartmentsTable
(
Department VARCHAR(50) NOT NULL,
Faculty VARCHAR(50) NOT NULL,
Building VARCHAR(50) NOT NULL,
CONSTRAINT PRIMARY KEY (Department)
) type = InnoDB;

CREATE TABLE ProfessorsTable
(
Name VARCHAR(50) NOT NULL,
Department VARCHAR(50) NOT NULL,
Rank VARCHAR(50) NOT NULL,
Salary INT UNSIGNED NOT NULL,
CONSTRAINT FOREIGN KEY (Department) REFERENCES DepartmentsTable(Department),
CONSTRAINT PRIMARY KEY (Name, Department)
) type = innoDB;

最佳答案

获取所有部门的平均工资。 . .

SELECT Department, avg(Salary) AS avg_salary
FROM ProfessorsTable
GROUP BY Department
ORDER BY avg_salary DESC;

只获取平均值最高的部门。 . .

SELECT Department, avg(Salary) AS avg_salary
FROM ProfessorsTable
GROUP BY Department
ORDER BY avg_salary DESC LIMIT 1;

关于mysql - 使用外键关系的 SQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16502627/

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