所以我试图从这两张表格中过滤掉我所有人的联合身份证。基本上,如果ID包含某个名称,我想将它们从查询中过滤掉。这是一个例子
ID,Name
1,Mark
1,John
2,Peter
2,Matt
3,Henry
3,Matt
4,John
4,Olaf
所以我不想包含包含' Matt'意思是我想完全过滤掉ID 2和3。
使用类似的东西:
select *
from name.table
where name not like 'Matt'
似乎只过滤了这一行。
答案 0 :(得分:1)
使用not exists
:
select n.*
from names n
where not exists (select 1 from names n2 where n2.id = n.id and n2.name = 'Matt')
答案 1 :(得分:0)
试试这个:
SELECT * FROM TableName
WHERE ID NOT IN (SELECT ID FROM TableName WHERE name='Matt')
或使用LIKE
:
SELECT * FROM TableName
WHERE ID NOT IN (SELECT ID FROM TableName WHERE name LIKE '%Matt%')
对于不同的值:
SELECT DISTINCT ID FROM TableName
WHERE ID NOT IN (SELECT ID FROM TableName WHERE name='Matt')
答案 2 :(得分:0)
你会想做这样的事情:
select * from table where id not in(
select Id from table where name like '%Matt%'
)
这将排除名称中包含Matt的行
排除等于Matt的行
select * from table where id not in(
select Id from table where name ='Matt'
)
答案 3 :(得分:0)
SELECT * FROM names
WHERE ID NOT IN
(SELECT ID FROM names WHERE name='Matt')
答案 4 :(得分:0)
(SELECT Column(s) FROM Table1)
UNION ALL
(SELECT Column(s) FROM Table2)
EXCEPT
(SELECT Column(s) FROM Table1 where name = 'Matt')
EXCEPT
(SELECT Column(s) FROM Table2 where name = 'Matt')