首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

使用sql语句team in (另一个表里的字段值)时提示异常

2012-01-07 
使用sql语句team in (另一个表里的字段值)时提示错误AllPath表里的FriendTeam值为字符型,值是1,4Friend表

使用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)

热点排行