'AND columnName NOT IN'在mysql中不起作用

时间:2019-01-26 09:52:03

标签: mysql

我已经建立了一个查询,如下所示

SELECT * 
FROM posts 
WHERE postUserId IN (SELECT userB 
                     FROM friends 
                     WHERE userA = 1) 
   OR postUserId IN (SELECT userA 
                     FROM friends 
                     WHERE userB = 1) 
  AND postUserId NOT IN (2) 
ORDER BY postId DESC

查询的所有行均显示我需要隐藏WHERE NOT IN,但结果与预期不符

1 个答案:

答案 0 :(得分:1)

您需要正确添加括号才能获得所需的结果

SELECT * 
FROM posts 
WHERE ( postUserId IN (SELECT userB 
                     FROM friends 
                     WHERE userA = 1) 
   OR postUserId IN (SELECT userA 
                     FROM friends 
                     WHERE userB = 1) 
    )
  AND postUserId NOT IN (2) 
ORDER BY postId DESC