问一个简单的问题,select问题

seesea125 2006-07-06 11:19:26

select * from bj_allnewssearchresult where
(322 in (type))


其中type是varchar类型,他的数据格式为
321,322,323
这样的格式

我想选择出来包含322的字段出来,却发现出了问题

1 如果select * from bj_allnewssearchresult where
(322 in (type)) 这么写提示:将 varchar 值 '321,322,323' 转换为数据类型为 int 的列时发生语法错误。

如果select * from bj_allnewssearchresult where
('322' in (type))
却有选择不出来

郁闷了,怎么解决?
谢谢各位大虾!!!!!!!
...全文
157 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
seesea125 2006-07-07
  • 打赏
  • 举报
回复
谢谢各位!!!,揭帖
fcuandy 2006-07-07
  • 打赏
  • 举报
回复
出错的原因在于:

你的type值为 321,322,323,它是一个字串,不是集合。在用数学知识理解为:
type="321,322,323"
而不是
type:{321,322,323}
用charindex,patindex,like都可以,不过楼上的like写的有问题,会误选数据出来。

create table test(col varchar(30))
insert into test values('321,322,323')
insert into test values('421,422322,423')
insert into test values('361,362,363')
go
select * from test where col like '%322%'
--执行后看能选出什么来
--应该是
--select * from test where ','+col+',' like '%,322,%'
drop table test

当然也可以用动态SQL,不过有点小题大做了。
wgsasd311 2006-07-07
  • 打赏
  • 举报
回复
create table test(col varchar(30))
insert into test values('321,322,323')
insert into test values('421,422,423')
insert into test values('361,362,363')
go
select * from test where col like '%322%'
drop table test
liangpei2008 2006-07-06
  • 打赏
  • 举报
回复
楼上正解!
LouisXIV 2006-07-06
  • 打赏
  • 举报
回复
select *
from bj_allnewssearchresult
where charindex(',322,',','+type+',')>0

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧