charindex('') > 0 只是查找comm_id中包含ID = '1'的,又不管逗号后面的 你可以先用substring截出逗号后面的,再用子查询查name, 写存储过程吧 select substring( comm_id, charindex(','+rtrim(id)+',',','+(select comm_id from order_info where ID = '1')+','), len(comm_id)) from order_info where charindex(','+rtrim(id)+',', ','+(select comm_id from order_info where ID = '1')+',') > 0
declare @str varchar(2000) select @str=isnull(comm_id,'')+crid from order_info where ID='1'select comm_name from comm_info where charindex(rtrim(id),@str)>0
select comm_name from comm_info where charindex(rtrim(id),(select STUFF ( select ','+ comm_id from order_info where ID = '1' for xml path('') ),1,1,''))>0
select comm_name from comm_info where id in (select substring( comm_id, charindex(','+rtrim(id)+',', ','+ comm_id + ','), len(comm_id)) from order_info where ID = '1')
这个貌似不行 comm_id是nchar类型的 运行以后报错如下"在将 nvarchar 值 '2,3,4,5' 转换成数据类型 int 时失败。"
这个程序里应该是怎么写的 我现在代码是这样的 public void getUserID() { string strsql = "select comm_name from comm_info where charindex(','+rtrim(id)+',',','+(select comm_id from order_info where ID = '1')+',')>0"; this.userID.Text = DBManager.executeGetReturn(strsql); }
public void getUserID() { string strsql = @"declare @str varchar(2000) select @str=isnull(comm_id,'')+crid from order_info where ID='1' select comm_name from comm_info where charindex(rtrim(id),@str)>0"; this.userID.Text = DBManager.executeGetReturn(strsql); }
我觉的我错了。你的表1存储就错了,应该是每一行只存一个购买序号,用户买了多本书,就存多行数据。如果你改不了表结构,要实现你的查询,用的我方法就是很复杂的,如下,希望有高手给你出更简单的方法。 首先要自己创建一个类似于Split功能的函数。 create FUNCTION [dbo].[udf_Split](@SourceText AS VARCHAR(max)) RETURNS @Table_Split Table ( RowIndex int not null, RowText varchar(max) null ) AS BEGIN declare @RowIndex int select @RowIndex = 0 while (charindex(',',@SourceText)>0) begin select @RowIndex = @RowIndex + 1; INSERT INTO @Table_Split(RowIndex,RowText) values(@RowIndex,substring(@SourceText,1,charindex(',',@SourceText)-1)); set @SourceText=substring(@SourceText,charindex(',',@SourceText)+1,len(@SourceText)) end if len(@SourceText) > 0 begin select @RowIndex = @RowIndex + 1; INSERT INTO @Table_Split(RowIndex,RowText) values(@RowIndex,@SourceText); end RETURN; END然后使用游标: create table #userbookname ( userid varchar(50), username varchar(8000) )declare @userid varchar(50) declare @bookid varchar(50) declare @bookname varchar(8000) declare cu_result cursor for select userid,bookid from userbook open cu_result fetch next from cu_result into @userid,@bookid while @@fetch_status=0 begin set @bookname=null select @bookname=isnull(@bookname,'')+','+bookname from book where bookid in (select RowText from dbo.udf_Split(@bookid)) insert into #userbookname(userid,username) select @userid,substring(@bookname,2,len(@bookname)) fetch next from cu_result into @userid,@bookid end close cu_result deallocate cu_resultselect * from #userbookname drop table #userbookname
测试数据: create table userbook ( userid varchar(50), bookid varchar(50) ) gocreate table book ( bookid int not null identity(1,1), bookname varchar(50) ) goinsert into userbook(userid,bookid) select '张三','2,3,4,5' union all select '李四','1,2,5' union all select '王五','1,2,5' goinsert into book(bookname) select '书名1' union all select '书名2' union all select '书名3' union all select '书名4' union all select '书名5' go最终的查询结果如下:
具体应该怎么写的 求指教 刚接触asp.net
你可以先用substring截出逗号后面的,再用子查询查name, 写存储过程吧
select substring(
comm_id,
charindex(','+rtrim(id)+',',','+(select comm_id from order_info where ID = '1')+','),
len(comm_id))
from order_info where charindex(','+rtrim(id)+',', ','+(select comm_id from order_info where ID = '1')+',') > 0
select @str=isnull(comm_id,'')+crid from order_info where ID='1'select comm_name from comm_info where charindex(rtrim(id),@str)>0
(select substring(
comm_id,
charindex(','+rtrim(id)+',', ','+ comm_id + ','),
len(comm_id))
from order_info where ID = '1')
这个貌似不行 comm_id是nchar类型的 运行以后报错如下"在将 nvarchar 值 '2,3,4,5' 转换成数据类型 int 时失败。"
这个程序里应该是怎么写的 我现在代码是这样的
public void getUserID()
{
string strsql = "select comm_name from comm_info where charindex(','+rtrim(id)+',',','+(select comm_id from order_info where ID = '1')+',')>0";
this.userID.Text = DBManager.executeGetReturn(strsql);
}
{
string strsql = @"declare @str varchar(2000)
select @str=isnull(comm_id,'')+crid from order_info where ID='1'
select comm_name from comm_info where charindex(rtrim(id),@str)>0";
this.userID.Text = DBManager.executeGetReturn(strsql);
}
高人 这个crid是什么 我把它换成comm_id还是只显示一个结果
高人 这个crid是什么 我把它换成comm_id还是只显示一个结果不好意思,错了,crid就是你的comm_id字段。
高人 这个crid是什么 我把它换成comm_id还是只显示一个结果不好意思,错了,crid就是你的comm_id字段。恩 我换成comm_id了 但结果还是一样 只显示comm_id中第一个值所对应的comm_name,后面几个值还是显示不出来
高人 这个crid是什么 我把它换成comm_id还是只显示一个结果不好意思,错了,crid就是你的comm_id字段。恩 我换成comm_id了 但结果还是一样 只显示comm_id中第一个值所对应的comm_name,后面几个值还是显示不出来你直接举个例子,
例如你的的两个数据库中分别有那些数据,然后你要得到什么结果。
假设数据如下:
订单表有数据:表1
序号 用户姓名 购买序号
1 张三 2,3,4,5
2 李四 1,2,5
3 王五 1,2,5产品表有数据:
表2
序号 商品名称
1 书名1
2 书名2
3 书名3
4 书名4
5 书名5现在想通过sqlserver查询得到这样的结果
序号 用户姓名 购买序号
1 张三 书名2,书名3,书名4,书名5
2 李四 书名1,书名2,书名5
3 王五 书名1,书名2,书名4
我觉的我错了。你的表1存储就错了,应该是每一行只存一个购买序号,用户买了多本书,就存多行数据。如果你改不了表结构,要实现你的查询,用的我方法就是很复杂的,如下,希望有高手给你出更简单的方法。
首先要自己创建一个类似于Split功能的函数。
create FUNCTION [dbo].[udf_Split](@SourceText AS VARCHAR(max))
RETURNS @Table_Split Table
(
RowIndex int not null,
RowText varchar(max) null
)
AS
BEGIN
declare @RowIndex int
select @RowIndex = 0
while (charindex(',',@SourceText)>0)
begin
select @RowIndex = @RowIndex + 1;
INSERT INTO @Table_Split(RowIndex,RowText)
values(@RowIndex,substring(@SourceText,1,charindex(',',@SourceText)-1));
set @SourceText=substring(@SourceText,charindex(',',@SourceText)+1,len(@SourceText))
end
if len(@SourceText) > 0
begin
select @RowIndex = @RowIndex + 1;
INSERT INTO @Table_Split(RowIndex,RowText)
values(@RowIndex,@SourceText);
end
RETURN;
END然后使用游标:
create table #userbookname
(
userid varchar(50),
username varchar(8000)
)declare @userid varchar(50)
declare @bookid varchar(50)
declare @bookname varchar(8000)
declare cu_result cursor for select userid,bookid from userbook
open cu_result
fetch next from cu_result into @userid,@bookid
while @@fetch_status=0
begin
set @bookname=null
select @bookname=isnull(@bookname,'')+','+bookname from book
where bookid in (select RowText from dbo.udf_Split(@bookid)) insert into #userbookname(userid,username)
select @userid,substring(@bookname,2,len(@bookname)) fetch next from cu_result into @userid,@bookid
end
close cu_result
deallocate cu_resultselect * from #userbookname
drop table #userbookname
create table userbook
(
userid varchar(50),
bookid varchar(50)
)
gocreate table book
(
bookid int not null identity(1,1),
bookname varchar(50)
)
goinsert into userbook(userid,bookid)
select '张三','2,3,4,5'
union all
select '李四','1,2,5'
union all
select '王五','1,2,5'
goinsert into book(bookname)
select '书名1'
union all
select '书名2'
union all
select '书名3'
union all
select '书名4'
union all
select '书名5'
go最终的查询结果如下: