SQL查询错误(NOT IN运算符)

时间:2012-12-08 13:03:44

标签: jquery mysql sql

我在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运算符上,但我不知道它在哪里。非常感谢帮助!

5 个答案:

答案 0 :(得分:2)

你需要一个逗号: - )

SELECT E.ID, E.name

答案 1 :(得分:1)

缺少逗号:

SELECT E.ID, E.name
           ^--

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

答案 2 :(得分: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);

逗号很重要。

答案 3 :(得分: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)

答案 4 :(得分: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
    )
    ;