尝试编写内部联接以过滤掉某些条件

Aid*_*den 2 sql sql-server inner-join

我目前正在努力进行一些加入,希望有人对此有所了解。我有三个表:A,B,C

  • 表C列出了个人名称
  • 表A列出了他们喜欢吃的食物

  • 表B是显示一个人喜欢C的食物的链接(我们的系统是在没有外键的情况下构建的!我知道,这很痛苦!)

我要写的是一个查询,该查询将返回表C中的值列表,该表显示了不喜欢特定食物的人...比如说PFC

我有以下几点:

select * from table_c c
inner join table_b b
on c.name = b.bValue
inner join table_a a
on b.aValue = a.number
where a.value not in('PFC')
Run Code Online (Sandbox Code Playgroud)

我假设联接正在工作,但是由于表A具有多个值,因此将返回另外两个行。如果其中一个联接显示我不想看到的食物,是否可以不显示此客户?

Table A
|---------------------|------------------|
|      Number         |     Value        |
|---------------------|------------------|
|          1          |       McDs       |
|---------------------|------------------|
|          1          |       KFC        |
|---------------------|------------------|
|          1          |       PFC        |
|---------------------|------------------|

Table B
|---------------------|------------------|
|      bValue         |     aValue       |
|---------------------|------------------|
|          John       |       1          |
|---------------------|------------------|

Table C
|---------------------|
|      Name           |
|---------------------|
|          John       |
|---------------------|
Run Code Online (Sandbox Code Playgroud)

如果这有所作为,我还将使用SQL Server 2013!

for*_*pas 5

有不存在:

select * from table_c c
where not exists (
  select 1 from table_b b inner join table_a a
  on b.aValue = a.number
  where b.bValue = c.name and a.value = 'PFC'
)
Run Code Online (Sandbox Code Playgroud)

  • *在我回答之后给出*。 (2认同)