我有两张桌子:
表1
Name | Surname
表2
Introduce | Type
表2中的介绍包含Table1中的Name或Surname或两者。我列出了Table1中介绍中没有的所有记录,并且工作得很好。
我尝试做的是使用Table2中的Type来限制这些结果。
我正在尝试这样的代码:
select t1.Name, t1.Surname
from Table1 t1
where not exists (select 1 Introduce
from Table2 t2
where (t2.Introduce like ('%' + t1.Name+ '%')
or t2.Introduce LIKE ('%' + t1.Surname + '%'))
)
and exists (select Type
from Table2
where Type in (90, 120))
但如果我使用and exists...
或不使用相同的结果,则没有区别。
答案 0 :(得分:0)
将WHERE子句更改为
where not exists
( SELECT 1 Introduce
from Table2 t2
where (t2.Introduce like ('%' + t1.Name+ '%') or t2.Introduce LIKE ('%' + t1.Surname + '%'))
and T2.Type in (90, 120)
)
您的设计需要改变。它不应该被非规范化
答案 1 :(得分:0)
select t1.Name, t1.Surname
from Table1 t1
where not exists
( SELECT 1 Introduce
from Table2 t2
where (t2.Introduce like ('%' + t1.Name+ '%') or t2.Introduce LIKE ('%' + t1.Surname + '%'))
and T2.Type in (90, 120)
)
答案 2 :(得分:0)
试试这个:
select name, surname from table1 [t1]
left join (select * from table2 where type in (90, 120)) [t2]
on CHARINDEX([t1].name, [t2].Introduce) > 0 or CHARINDEX([t1].surname, [t2].Introduce) > 0
where [t2].introduce is null