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

使用sql语句team in (另一个表里的字段值)时提示错误
AllPath表里的FriendTeam值为字符型,值是1,4
Friend表里的team值为整型,每条记录有1,有4,也有其它的值

select   *   from   Friend   where   Team   in   (AllPath.FriendTeam)

提示将   varchar   值   '1,4 '   转换为数据类型为   int   的列时发生语法错误。

如果写成
select   *   from   Friend   where   Team   in   (1,4)
则没有问题,其实我的FriendTeam值里面就是1,4

------解决方案--------------------
select * from Friend t where exists
(Select * from AllPath where
Charindex( ', '+ltrim(t.Team)+ ', ', ', '+FriendTeam+ ', ')> 0)
------解决方案--------------------
--try


select * from Friend
where exists(select 1 from AllPath where charindex( ', '+rtrim(Friend.team)+ ', ', ', '+FriendTeam+ ', ')> 0)