2013-04-07 101 views
0

# tables ans resulting table加入多個表格並創建一個表格

我有3張名爲Advance,Transport和Medicine的表格。我想加入這三個表,並拿出一個像emp_id 1的最後一個表。是否有可能使用dql(doctrine)或sql在sql級別執行此操作?

+0

因爲y你不需要複製你的數據,我建議你改用'VIEW'。 – 2013-04-07 05:27:22

+0

由於某種原因,它們被稱爲RELATIONAL數據庫;所有的表都是關係,所有的視圖都是關係,所有的查詢都是關係,但是任何特定的關係都不一定是查詢,視圖或表格;只是一些關係的實現。 – 2013-04-07 05:48:26

回答

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