无法在语句中使用多个

时间:2016-12-12 04:39:49

标签: mysql exists notin

我希望找到练习和游戏表中不存在的条目,但我无法使用第二个不在语句中。还有其他办法吗?

 select VenueID from Venue
 where VenueID not in (select VenueID from Practice)
 and not in (select VenueId from Game)

2 个答案:

答案 0 :(得分:1)

您需要重复NOT IN语句,包括列名:

SELECT VenueID
FROM Venue
WHERE VenueID NOT IN (SELECT VenueID FROM Practice) AND
      VenueID NOT IN (SELECT VenueId FROM Game)
       ^^^ you omitted the second column name

使用NOT IN的另一种方法是做两个左连接:

SELECT VenueID
FROM Venue t1
LEFT JOIN Practice t2
    ON t1.VenueID = t2.VenueID
LEFT JOIN Game t3
    ON t1.VenueID = t3.VenueID
WHERE t2.VenueID IS NULL AND
      t3.VenueID IS NULL

答案 1 :(得分:0)

您在第二个not in中缺少列名称(每个条件都是单独的,语法是< column> NOT IN< value set> ):

select VenueID from Venue
where VenueID not in (select VenueID from Practice)
and VenueID not in (select VenueId from Game)
--  ^^^^^^^