2012-12-08 47 views
0

我試圖使用臨時表來簡化我的查詢,但我收到錯誤消息。 這裏是我的查詢:如何在SQL中使用臨時表

SELECT E.ID, E.name 
INTO #TmpEmplyee 
FROM Employee E, Outsourcing O, Student S 
WHERE E.ID NOT IN (SELECT ID FROM Student UNION SELECT ID FROM Outsourcing) 

SELECT R.name, I.startDate, T.name 
FROM Role R, Inn I, #TmpEmplyee T 
WHERE I.name = R.name AND I.ID = T.id 

和錯誤是:

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM Employee E, Outsourcing O, Student S WHERE E.ID NOT IN (SELECT ID FROM Stud' at line 3 

感謝您的幫助!

+0

http://dev.mysql.com/doc/refman/5.0/en/ansi-diff-select-into-table.html mysql不支持'select ... into' 。它有'insert .. select from' –

回答

0

要從中選擇,你應該創建一個臨時表使用此語法

CREATE TEMPORARY TABLE mytemptable 
SELECT Field1, Field2 
FROM mytable; 
1

在mysql中,您需要在查詢之前創建臨時表。還你不需要外包O,學生中的S FROM因爲你沒有從他們中選擇:

INSERT INTO #TmpEmplyee 
SELECT E.ID, E.name 
FROM Employee E 
WHERE E.ID NOT IN (SELECT ID FROM Student UNION SELECT ID FROM Outsourcing) 
1

試試這個,

SELECT R.name, I.startDate, T.name 
FROM Role R 
     INNER JOIN Inn I 
      ON I.name = R.name 
     INNER JOIN 
     (
      SELECT E.ID, E.name 
      -- remember that the join below produces CROSS JOIN 
      FROM Employee E, Outsourcing O, Student S 
      WHERE E.ID NOT IN (SELECT ID FROM Student 
           UNION 
           SELECT ID FROM Outsourcing) 
     ) T ON I.ID = T.id