2017-06-13 97 views
-1

我想從另一箇舊表格的某些列創建表格。舊錶中的每列都有149行,但是當我創建新表時,生成的表有1442行,這是錯誤的。如何從另一箇舊表格創建新表格

我的代碼:

create table newenronss as 
SELECT employeelist.eid 
     ,employeelist.firstName 
     ,employeelist.lastName 
     ,employeelist.Email_id 
     ,employeelist.status 
     ,message.mid 
     ,message.subject 
     ,message.body 
     ,message.folder 
     ,recipientinfo.rid 
     ,recipientinfo.rvalue 
     ,referenceinfo.reference 
FROM employeelist 
INNER JOIN message  ON employeelist.Email_id = message.sender 
INNER JOIN recipientinfo ON message.mid = recipientinfo.mid 
INNER JOIN referenceinfo ON recipientinfo.rid = referenceinfo.rfid 

我怎樣才能解決這個問題?

+1

代碼中沒有任何錯誤。也許你的邏輯存在缺陷。 – Strawberry

+0

有關舊錶的信息位於:http://www.ahschulz.de/enron-email-data/ .i想將此表的某些列複製到一個新表 – hsi

+1

可能是某個關鍵字有誤或匹配與更多的行..檢查連接條件.. – scaisEdge

回答

1

考慮以下幾點:

SELECT * FROM table_a; 
+----+ 
| id | 
+----+ 
| 1 | 
| 2 | 
| 3 | 
| 4 | 
+----+ 
4 rows in set (0.00 sec) 

SELECT * FROM table_b; 
+-----+------+ 
| id | a_id | 
+-----+------+ 
| 101 | 1 | 
| 102 | 1 | 
| 103 | 2 | 
| 104 | 4 | 
| 105 | 4 | 
| 106 | 4 | 
| 107 | 4 | 
+-----+------+ 
7 rows in set (0.00 sec) 

SELECT * FROM table_c; 
+------+------+ 
| id | a_id | 
+------+------+ 
| 1001 | 2 | 
| 1002 | 3 | 
| 1003 | 3 | 
| 1004 | 4 | 
| 1005 | 5 | 
| 1006 | 6 | 
| 1007 | 7 | 
| 1008 | 2 | 
| 1009 | 4 | 
+------+------+ 
9 rows in set (0.02 sec) 

SELECT * 
    FROM table_a a 
    JOIN table_b b 
    ON b.a_id = a.id 
    JOIN table_c c 
    ON c.a_id = b.a_id; 
+----+-----+------+------+------+ 
| id | id | a_id | id | a_id | 
+----+-----+------+------+------+ 
| 2 | 103 | 2 | 1001 | 2 | 
| 4 | 104 | 4 | 1004 | 4 | 
| 4 | 105 | 4 | 1004 | 4 | 
| 4 | 106 | 4 | 1004 | 4 | 
| 4 | 107 | 4 | 1004 | 4 | 
| 2 | 103 | 2 | 1008 | 2 | 
| 4 | 104 | 4 | 1009 | 4 | 
| 4 | 105 | 4 | 1009 | 4 | 
| 4 | 106 | 4 | 1009 | 4 | 
| 4 | 107 | 4 | 1009 | 4 | 
+----+-----+------+------+------+ 
10 rows in set (0.02 sec) 

所以,表A,B,和c分別具有4行,第7行,和第9行。但是當我們將它們連接在一起時,我們會得到10行 - 因此表中的行數與結果中的行數之間沒有明顯的相關性。

相關問題