繁体   English   中英

用于选择 a 或 b 但不选择 c 的 MySQL 查询

[英]MySQL query for selecting a or b but not c

我有一个包含 3 列的表:branchId、repoId 和 userId。 我需要找到为 repo 1 或 repo 2 创建分支但不是为 repo 3 创建分支的用户。任何帮助将不胜感激。

我尝试执行以下操作:

select userId 
  from branch 
 where repoId=1 
    or repoId=2 
   and repoId<>3

但是,这也会输出具有 repoId 3 的 userId,因为具有该 userId 的其他行没有 repoId 3。

例如,对于 userid 2,有 3 行,repoId 为 1、2 和 3。

上面查询的输出仍然给我结果为 userId 2 因为它的 2 行没有 repoId 3。

示例表:

branchId repoId userId 1 1 1 2 2 1 3 1 2 4 2 2 5 3 2

所以对于这个表,结果应该是 userId 1。

尝试类似下面的内容。

select userId 
from branch 
where repoId in(1,2) AND userid not in(select userid from branch where repoId =3)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM