需要帮助来生成SQL查询

时间:2018-05-14 13:10:02

标签: sql

有两个表Employee和Department

员工表

 EmpID  EmpName
 E001   Jack
 E002   Jill

部门表

DeptID  DeptName
D001    IT
D002    HR

结果

ID     Name      Type
E001    Jack    Employee
E002    Jill    Employee
D001    IT      Department
D002    HR      Department

如何添加第三列?

3 个答案:

答案 0 :(得分:0)

使用修正值:

select EMPID, EMPNAME, 'Employee' as TYPE from Employee 
Union all
select DeptID  , DeptName, 'Department'  as TYPE from Department 

答案 1 :(得分:0)

您是否尝试过union all

select EmpID as ID, EmpName as Name, 'Employee' as Type
from Employee e union all
select DeptID , DeptName, 'Department'
from Department d;

答案 2 :(得分:0)

SELECT EmpID AS ID, EmpName AS Name, 'Employee' AS [Type]
FROM Employees
UNION ALL
SELECT DeptID AS ID, DeptName AS Name, 'Department' AS [Type]
From Departments

我建议更换列名称" Type"带有非保留字。