select * from Scenic where SortID  like  in (select id from XXXX);
这句肯定是错的... 我想知道对的怎么写...

解决方案 »

  1.   

    SortID  里面存的字符串 逗号隔开的 你懂得..
      

  2.   


    select * from Scenic  t
    where exists (select 1 from XXXX where charindex(','+ltrim(id)+',',','+t.sortid+',') > 0)
      

  3.   

    大神  敢问这是神马意思select 1 from XXXX where charindex(','+ltrim(id)+',',','+t.sortid+',') > 0
      

  4.   

    0.0  我错了,去小忙了下。charindex 是看字符串1在字符串2中存在的为止,不存在返回0,可以看看函数的介绍至于 ','+ltrim(id)+',' 和后边的 ','+t.sortid+',' 是为了避免模糊查询时数据多查的问题,例如id 是 1 2 3sortid 是 1,21如果直接 charindex(ltrim(id),sortid) 那么id是2的在sortid中也是存在位置的,返回3大于0,但 ,2, 在sortid中是没有位置的,避免21中的2匹配到id为2的数据。
      

  5.   

     ','+ltrim(id)+','的话 
    假如
    id 是 1 2 3
    sortid 是 1,21 
    如果模糊查询   1 
    他找的岂不是  ,1, 在
    sortid 里面确是 1,  
    会不会查不到啊
      

  6.   

    ','+ltrim(id)+','是不是应该改为ltrim(id)+',' ???