Select * from food_com where ',' + ctypeid +  like '%,1,%' order by cnum desc

解决方案 »

  1.   

    select * from food_com where charindex(','+cast(id as varchar)+',',ctypeid)>0
    order by cnum desc
      

  2.   

    --上面寫的有點問題--方法一
    Select * from food_com where ',' + ctypeid + ',' like '%,1,%' order by cnum desc
    --方法二
    Select * from food_com where CharIndex(',1,', ',' + ctypeid +  ',') > 0 order by cnum desc
      

  3.   

    Create Table food_com
    (cnum Int,
     ctypeid Varchar(100))
    Insert food_com Select 1, '1,2,3'
    Union All Select 2, '11,2,4,5'
    Union All Select 3, '21,23,4,6'
    GO
    Declare @id Int
    Select @id = 1
    --方法一
    Select * from food_com where ',' + ctypeid + ',' like '%,' + Rtrim(@id) +',%' order by cnum desc
    --方法二
    Select * from food_com where CharIndex(',' + Rtrim(@id) +',', ',' + ctypeid +  ',') > 0 order by cnum desc
    GO
    Drop Table food_com
    --Result
    /*
    cnum ctypeid
    1 1,2,3
    */