日期:2014-05-19  浏览次数:20578 次

查询语句的in问题,在外部程序里挺简单,但在查询分析器里好象比较难弄~
表:   字段
t1:   f1   varchar(20)
t2:   id     int

t1的f1字段存放t2   id   的逗号分割字符串
比如
t1中有一条记录的f1为     12,13

我想由t1关联到t2,使用以下语句.

select   *   from   t2   where   id
in   (select   f1   from   t1   where   ...   )        

问题来了:
12,13
服务器:   消息   245,级别   16,状态   1,行   4
将   varchar   值   '12,13 '   转换为数据类型为   int   的列时发生语法错误。

我想,它一定是解析成了
select   *   from   t2   where   id   in   (   '12,13 '   )  
而不是   我要的select   *   from   t2   where   id   in   (   12,13   )
请问有什么办法(在不改变字段类型的情况下)解决,多谢

------解决方案--------------------
select * from t2 where charindex( ', '+rtrim(id)+ ', ' , ', '+ '12,13 '+ ', ')
------解决方案--------------------
Select T2.* From T2
Inner Join T1
On CharIndex( ', ' + Rtrim(T2.id) + ', ', ', ' + T1.f1 + ', ') > 0
Where ...