mssql sqlserver 使用like,对两张表进行嵌套模糊查询


摘要:
最近接收到用户需求,需将一张表的字段内容包含另外一张表的字段内容的数据嵌套查询出来,
下文展示三种嵌套查询的实现方式


例:

 /*
下文将演示使用它表的字段作为当前表查询的like内容
*/
create table A(keyIdA int,infoA varchar(30))
create table B(keyIdB int,infoB varchar(60))
go
insert into A(keyIdA,infoA)values(1,'maomao365.com')
insert into A(keyIdA,infoA)values(2,'猫猫小屋')
insert into A(keyIdA,infoA)values(3,'测试表嵌套查询') 
go
insert into B(keyIdB,infoB)values(10,'猫猫')
insert into B(keyIdB,infoB)values(20,'嵌套') 
go
---方法1:使用 charindex replace 

select * from A where exists(select null from B where   charindex(B.infoB,A.infoA) >0)

select * from A where exists(select null from B where   len(A.infoA)>len(replace(A.infoA,B.infoB,'')) )

---方法2:: 使用like 嵌套查询
select *from A where exists(select null from B where A.infoA like '%'+b.infoB+'%')


go
drop table A
drop table B
2.mssql_sqlserver_表嵌套查询分享

2.mssql_sqlserver_表嵌套查询分享