尝试合并表

时间:2018-05-02 14:37:19

标签: mysql ms-access

对不起,如果它是一个非常简单的新手。我一直试图将两张桌子结合起来,但是无法完成它。尝试将员工人数添加到另一个表中。

Table Q9r Other 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)
;

2 个答案:

答案 0 :(得分:1)

你可以通过这种方式轻松组合2个表:

-- 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

答案 1 :(得分:0)

FROM Q9r,WHERE - > ,是未经请求的。