两张表:
test1:
id name (id int ,name varchar)
1 test1
2 test2
3 test3
test2:
name id (name varchar, id varchar)
test1 1
test2 1,2查询test2,id在test1包含的内容,结果:
id name
1 test1
2 test2
test1:
id name (id int ,name varchar)
1 test1
2 test2
3 test3
test2:
name id (name varchar, id varchar)
test1 1
test2 1,2查询test2,id在test1包含的内容,结果:
id name
1 test1
2 test2
From test1 A
Inner Join test2 B On CharIndex(',' + Cast(A.id As Varchar) + ',', ',' + B.id + ',') > 0
ORSelect Distinct A.*
From test1 A
Inner Join test2 B On ',' + B.id + ',' Like '%,' + Cast(A.id As Varchar) + ',%'
(id int ,name varchar(10))
Insert test1 Select 1, 'test1'
Union All Select 2, 'test2'
Union All Select 3, 'test3'
Create Table test2
(name varchar(10), id varchar(100))
Insert test2 Select 'test1', '1'
Union All Select 'test2', '1,2'
GO
--方法一
Select Distinct A.*
From test1 A
Inner Join test2 B On CharIndex(',' + Cast(A.id As Varchar) + ',', ',' + B.id + ',') > 0--方法二
Select Distinct A.*
From test1 A
Inner Join test2 B On ',' + B.id + ',' Like '%,' + Cast(A.id As Varchar) + ',%'
GO
Drop Table test1, test2
--Result
/*
id name
1 test1
2 test2
*/