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)

我有这个例外:

#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 '.name FROM Employee E, Outsourcing O, Student S WHERE E.ID NOT IN (SELECT ID FRO' at line 1

我试图运行内部查询并且它有效。我认为问题出在NOT IN操作员上,但我不知道它在哪里。帮助将不胜感激!

4

5 回答 5

2

你需要一个逗号:-)

SELECT E.ID, E.name
于 2012-12-08T13:05:37.233 回答
1

缺少逗号:

SELECT E.ID, E.name
           ^--

现在您正在尝试将 定义E.name为别名E.ID,但别名中不能有句点。

于 2012-12-08T13:05:53.853 回答
1
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);

逗号很重要。

于 2012-12-08T13:06:15.420 回答
1

您需要在查询之间使用逗号

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)
于 2012-12-08T13:07:02.470 回答
1

最好避免使用 IN (NOT IN 会使用 NULL 值做意想不到的事情,(但由于这些“关键字段”可能不可为空,这在这里不会成为问题),并避免使用 UNION (UNION 需要删除重复项,因此可能需要排序两次)),尤其是在不需要它们的情况下;-)

注意:下面的查询可能“短路”(如果其中一个产生 True,则不需要评估两个 EXISTS),因此它可能需要在子查询中获取比 UNION 变体更少的行。

SELECT E.ID,E.name
-- into #TmpEmplyee
FROM Employee E
WHERE NOT EXISTS (
    SELECT * 
    FROM Student s
    WHERE s.ID=E.ID
    )
AND NOT EXISTS (
    SELECT *
    FROM Outsourcing o
    WHERE o.ID=E.ID
    )
    ;
于 2012-12-08T13:16:27.087 回答