mySQL:如何选择FROM表WHERE IN LIST而不是另一个列表

wax*_*cal 3 mysql select

是否有可能从表中选择值,它们不存在于一个列表中,但存在于另一个列表中......或者它们是否相反?

例如

SELECT COUNT(g.`property`) as `number`, g.`property` 
  FROM `foo` g 
 WHERE `theID` IS IN (SELECT `theID` 
                        FROM `tableofIDS` 
                       WHERE `theID` = '54252') 
          AND NOT IN (SELECT `theID` 
                        FROM `anotherTableofIDS` 
                       WHERE `theID` = '54252')
Run Code Online (Sandbox Code Playgroud)

The*_*ter 9

SELECT COUNT(g.`property`) as `number`, g.`property`
FROM `foo` g
WHERE `theID` IN (SELECT `theID` FROM `tableofIDS` WHERE `theID` = '54252')
  AND `theID` NOT IN (SELECT `theID` FROM `anotherTableofIDS` WHERE `theID` = '54252')
GROUP BY g.`property`
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用性能更好的联接:

SELECT COUNT(g.`property`) as `number`, g.`property`
FROM `foo` g JOIN (
    SELECT `theID`
    FROM `tableofIDS`
    WHERE `theID` = '54252'
   ) id1 ON g.theID = id1.theID
  LEFT JOIN (
    SELECT `theID`
    FROM `anotherTableofIDS`
    WHERE `theID` = '54252'
  ) id2 ON g.theID = id2.theID
WHERE id2.theID IS NULL
GROUP BY g.`property`
Run Code Online (Sandbox Code Playgroud)