两张桌子 - '薪水'和'master1'
薪水是按年计算的,我可以将它们分组,以便使用
获得每位玩家的总和SELECT playerID, sum(salary) as sal
FROM salaries
GROUP BY playerID ORDER BY sal DESC LIMIT 10;
这将返回playerID和工资总和,但我需要'name1'和'nameLast'列下'master1'表中的玩家姓名。他们在'master1'和'薪水'中都有'playerID'的公共列,但是当我尝试运行时
SELECT master1.nameFirst, master1.nameLast, sum(salary) as sal
FROM salaries, master1
GROUP BY salaries.playerID ORDER BY sal DESC LIMIT 10;
我收到错误
表达式不在GROUP BY
键'nameFirst'
我尝试过修补它以继续收到错误。
谢谢!
答案 0 :(得分:0)
需要在nameFirst
中包含nameLast
和group by
:
SELECT
master1.nameFirst,
master1.nameLast,
sum(salary) as sal
FROM salaries JOIN master1 ON salaries.playID = master1.playerID
GROUP BY master1.nameFirst, master1.nameLast, salaries.playerID
ORDER BY sal DESC LIMIT 10;
答案 1 :(得分:0)
首先,您需要使用正确的显式JOIN
语法
SELECT
MAX(m.nameFirst) FirstName,
MAX(m.nameLast) LastName,
SUM(s.salary) Salary
FROM master1 m
INNER JOIN salaries s ON m.playerID = s.playerID
GROUP BY m.playerID
使用master1
表获取 FirstName , LastName ,然后执行JOIN
salaries
表来获取每个玩家的总薪水。
对于使用GROUP BY
子句时的当前查询异常,请确保SELECT
语句中的 columns / expressions 需要聚合。