尝试合并表时发生访问错误

ih99xse1  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(432)

抱歉,如果事情真的很简单,我是个新手。我一直想把两张table合并起来,但做不成。正在尝试将雇员数添加到另一个表中。

SELECT [Training History].JobID,
    (
    SELECT Job.Department 
        FROM Job 
        WHERE [Training History].JobID = Job.JobID) AS Department, 
        SUM([Expenditure]) AS ExpenditureOfJob 
    FROM [Training History] 
    GROUP BY [Training History].JobID,
    (
    SELECT [Number of Employees] 
    FROM Q9r, 
    WHERE Q9r.JobID = [Training History].JobID
    )
;

这使我在上一个select语句中出错(请帮助:)
q9r表格代码为;

SELECT JobID, COUNT (*) AS [Number of Employees] FROM Employee GROUP BY JobID;
enter code here

也不能让它工作(这是我比较喜欢的方式,因为它可以在一个表中完成)

SELECT [Training History].JobID,(
       SELECT Job.Department 
       FROM Job 
       WHERE [Training History].JobID = Job.JobID) 
       AS Department, 
SUM([Expenditure]) AS ExpenditureOfJob 
     FROM [Training History] 
     GROUP BY [Training History].JobID,
(SELECT COUNT (*) AS [Number of Employees] 
     FROM Employee 
     GROUP BY JobID)
;

这也不行

SELECT [Training History].JobID,(SELECT Job.Department FROM Job WHERE [Training History].JobID = Job.JobID) AS Department, 
SUM([Expenditure]) AS ExpenditureOfJob FROM [Training History] GROUP BY [Training History].JobID,
(SELECT COUNT (*) AS [Number of Employees] 
     FROM Employee 
    WHERE [Training History].JobID = Employee.JobID
 GROUP BY JobID)
;
8ehkhllq

8ehkhllq1#

来自q9r,其中-->是未经请求的。

disho6za

disho6za2#

您可以这样轻松地组合两个表:

-- My way to combinates 2 tables:
SELECT Department.JobID, Department.NameDepartment,
       Department.ExpenditureOfJob, Job.NumberOfEmployees 
FROM Department INNER JOIN Job 
ON Department.JobID = Job.JobID
GROUP BY Department.JobID;

顺便说一下,您的查询可以适应以下形式:

-- I suppose that the Training History & Expenditure both are the Databases_Name... 
SELECT [Training History].JobID, Job.Department AS Department, SUM([Expenditure]) AS ExpenditureOfJob 
FROM [Training History].Department INNER JOIN [Training History].JobID 
GROUP BY [Training History].JobID,( SELECT COUNT (*) AS [Number of Employees] 
                                    FROM Employee 
                                    GROUP BY JobID);

在任何情况下,我都把测试放在sql fiddle中

相关问题