两张表:
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

解决方案 »

  1.   

    Select Distinct A.*
    From test1 A
    Inner Join test2 B On CharIndex(',' + Cast(A.id As Varchar) + ',', ',' + B.id + ',') > 0
      

  2.   


    ORSelect Distinct A.*
    From test1 A
    Inner Join test2 B On ',' + B.id + ',' Like '%,' + Cast(A.id As Varchar) + ',%'
      

  3.   

    Create Table test1
    (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
    */
      

  4.   

    思路,test1的id前后加上','号,再匹佩test2的id前后加上','号的字段
      

  5.   

    select * from test2 where exists(select * from test1 where test1.id=test2.id)
      

  6.   

    select * from test1 where exists(select * from test2 where charindex(cast(test1.id as varchar(20)),test2.id)>0)