我有一张桌子,其中我有一个针对FK的多个条目.我想找出没有特定条目的FK的值,例如
我的表有以下条目.
PK----------------FK-----------------Column entries 1----------------100-----------------ab1 2----------------100-----------------ab2 3----------------100-----------------ab4 4----------------200-----------------ab1 5----------------200-----------------ab2 6----------------200-----------------ab3 7----------------300-----------------ab1 8----------------300-----------------ab2 9----------------300-----------------ab3 10---------------300-----------------ab4
现在,从这张表中我想过滤所有那些没有ab3或ab4的FK.当然,我期望不同的值,即在这种情况下结果将是FK = 100和200.
我正在使用的查询是
select distinct(FK)
from table1
where column_entries != 'ab3'
or column_entries != 'ab4';
Run Code Online (Sandbox Code Playgroud)
当然,此查询不会获取所需的结果.
尝试以下方法: -
select distinct fk_col from table1
minus
(select distinct fk_col from table1 where col_entry='ab3'
intersect
select distinct fk_col from table1 where col_entry='ab4')
Run Code Online (Sandbox Code Playgroud)
这将显示所有没有'ab3'和'ab4'的FK.即你的情况下100和200