MySQL 平均值和总计

MySQL average and total

这是我的代码,但所有值都相同。

SELECT 
    department.name, 
    AVG(salary) AS Average, 
    SUM(salary) AS Total
FROM 
    department, employee
GROUP BY 
    department.name


您缺少表之间的连接条件,因此您将它们组合为一个完整的交叉产品,而不是仅将员工链接到他们的部门。

SELECT d.name, AVG(e.salary), SUM(e.salary)
FROM department AS d
JOIN employee AS e ON d.dpt_no = e.dpt_no
GROUP BY d.name