select * from a,b
where charindex(','+cast(a.types as varchar)+',',','+b.tt+',')>0

解决方案 »

  1.   

    先用replace函数,之后在去比较吧
      

  2.   

    select * from b where charindex(',15,',','+tt+',')>0
      

  3.   

    select a.*,b.tt from a
    ,(select tt,tt1=','+tt+',' from b) c
    where charindex(','+cast(a.types as varchar)+',',c.tt1)>0
      

  4.   

    --测试数据
    create table A(types int)
    insert A select 15create table B(tt varchar(20))
    insert B  select '1'
    union all select '15'
    union all select '22'
    union all select '13,45'
    union all select '18,15'
    union all select '112,556,45,15'
    go--查询
    select * from a,b
    where charindex(','+cast(a.types as varchar)+',',','+b.tt+',')>0
    go--删除测试
    drop table a,b/*--测试结果types       tt                   
    ----------- -------------------- 
    15          15
    15          18,15
    15          112,556,45,15(所影响的行数为 3 行)
    --*/
      

  5.   

    to: zjcxc(邹建) 
    如果我不是和A表中的字段比较,而是和一个变量比较,这样写怎么不对?
    select * from a,b
    where charindex(','+cast(变量名称 as varchar)+',',','+b.tt+',')>0怎么会不对
    异常详细信息: System.Data.SqlClient.SqlException: 列名 'types' 无效。
    它把这个变量当作列名处理,是怎么回事?
      

  6.   

    你在那里引用了 types ?
      

  7.   

    --晕,看来你不会定义变量declare @types int
    set @types=15
    select * from b
    where charindex(','+cast(@types as varchar)+',',','+tt+',')>0