日期:2014-05-17  浏览次数:20845 次

小白求助 求高人看看 这句查询语句有什么问题
string strsql = "select comm_name from comm_info where charindex(','+rtrim(id)+',',','+(select comm_id from order_info where ID = '1')+',')>0";

comm_name的id对应order_info表中的comm_id,comm_id是vchar类型,有多个值并且用逗号隔开.现在查询只显示一个comm_id对应的comm_name,逗号之后的其他几个comm_id所对应的comm_name都显示不出来,求高人指点


急!!!!在线等!!!!

------解决方案--------------------
测试数据:
create table userbook
(
userid varchar(50),
bookid varchar(50)
)
go

create table book
(
bookid int not null identity(1,1),
bookname varchar(50)
)
go

insert into userbook(userid,bookid)
select '张三','2,3,4,5'
union all
select '李四','1,2,5'
union all
select '王五','1,2,5'
go

insert into book(bookname)
select '书名1'
union all
select '书名2'
union all
select '书名3'
union all
select '书名4'
union all
select '书名5'
go

最终的查询结果如下: