我有 3 张桌子,名为 Advance、Transport 和 Medicine。我想加入这三个表,并为 emp_id 1 提供一个类似于最后一个表的表。是否可以使用 dql(doctrine)或 sql 在 sql 级别执行此操作?
问问题
653 次
1 回答
0
CREATE TABLE NewTable
SELECT Date,
Emp_ID,
Amount As Advance,
NULL AS Transport,
NULL AS Medicine
FROM Advance
WHERE Emp_ID = 1 -- remove this WHERE clause to include all emp_ID
UNION ALL
SELECT Date,
Emp_ID,
NULL As Advance,
Amount AS Transport,
NULL AS Medicine
FROM Transport
WHERE Emp_ID = 1 -- remove this WHERE clause to include all emp_ID
UNION ALL
SELECT Date,
Emp_ID,
NULL As Advance,
NULL AS Transport,
Amount AS Medicine
FROM Medicine
WHERE Emp_ID = 1 -- remove this WHERE clause to include all emp_ID
您也可以VIEW
在此创建一个,
CREATE VIEW EmployeeView
AS
SELECT Date,
Emp_ID,
Amount As Advance,
NULL AS Transport,
NULL AS Medicine
FROM Advance
WHERE Emp_ID = 1 -- remove this WHERE clause to include all emp_ID
UNION ALL
SELECT Date,
Emp_ID,
NULL As Advance,
Amount AS Transport,
NULL AS Medicine
FROM Transport
WHERE Emp_ID = 1 -- remove this WHERE clause to include all emp_ID
UNION ALL
SELECT Date,
Emp_ID,
NULL As Advance,
NULL AS Transport,
Amount AS Medicine
FROM Medicine
WHERE Emp_ID = 1 -- remove this WHERE clause to include all emp_ID
于 2013-04-07T05:26:55.957 回答