在某表中有一字段column类型为char,其值为3,4,5
我想让它作为查询条件条件
select * from onttable where temp in (select column from anothertable)
但是sql会将3,4,5看作'3,4,5',把它作为整体,所以查询不出结果。请大家问如何处理。
感谢^_^

解决方案 »

  1.   

    select * from onttable a,(select column from anothertable) b
    where a.column  = b.column
      

  2.   

    create table test
    (
    id int identity(1,1),
    name varchar(10)
    )
    insert test select '3,4,5'
    union all select 'a'
    union all select 'c'
    union all select 'b'
    union all select 'd'
    declare @sql varchar(100)
    select @sql=replace(''''+name+'''',',',''',''') from test where id=1
    exec('select * from test where id in('+@sql+')')
    drop table test
      

  3.   

    select * from onttable a,  anothertable bwhere chaindex(',' + a.temp + ',',b.column) > 0 or chaindex(',' + a.temp ,b.column) > 0 or chaindex(a.temp + ',',b.column) > 0 
    查charindex函数,不清楚哪个放在前面思路就是只要里面包含了就可以,不过必须加',',因为 '3'也是包含在'34'里面的
      

  4.   

    to
    starsong(流风) 
    Sorry,不行啊
      

  5.   

    wait,我在试试,ok就结贴
    谢谢大火
      

  6.   

    select ''''+replace('3,4,5',',',''',''')+''''
    ---------------------------
    '3','4','5'所以可以写成:
    select * from onttable where temp in (select ''''+replace(column,',',''',''')+''''
     from anothertable)