+ -
当前位置:首页 → 问答吧 → 请教关于sql server检索中 in 使用时重复的id也必须检索

请教关于sql server检索中 in 使用时重复的id也必须检索

时间:2011-12-08

来源:互联网

在一个表中有一个字段 docid,现在希望检索出docid在以下字符串中的所有数据

使用 where [DocID] in (1,3783,1,5,5,106,368,371,1376)

但对于其中重复出现的数字 1 和 5 希望是两条数据,而不是只出现一条,而且 还必须按照数字出现的顺序排列检索结果

该如何处理??

如果不考虑重复数据可以用:
where [DocID] in (1,3783,1,5,5,106,368,371,1376) order by charindex(','+ltrim([DocID])+',',','+'1,3783,1,5,5,106,368,371,1376'+',')

但这样 1 和 5 只被检索了一次

现在一定需要出现几次就检索得到几次,而且按照顺序

谢谢

作者: chilli6519   发布时间: 2011-12-08

用 in,如果列表中有重复数据,并非只检出两条,而是所有的都被检出,因为后一个毫无意义.
这只能说明你还没有理解 in 操作的含义.

作者: qianjin036a   发布时间: 2011-12-08

你有几条数据,就检索几条,in后面重复没用

作者: chuanzhang5687   发布时间: 2011-12-08

in(1,1,5,5)
等价于:
in(1,5)

作者: qianjin036a   发布时间: 2011-12-08

谢谢,那我怎样样才能得到我希望的结果

重复的数字重复出现呢?

引用 3 楼 qianjin036a 的回复:

in(1,1,5,5)
等价于:
in(1,5)

作者: chilli6519   发布时间: 2011-12-08

你执行一下就知道了啊,有重复的,直接就显示了。

作者: fuxiyang   发布时间: 2011-12-08

引用 4 楼 chilli6519 的回复:

谢谢,那我怎样样才能得到我希望的结果

重复的数字重复出现呢?

引用 3 楼 qianjin036a 的回复:

in(1,1,5,5)
等价于:
in(1,5)


请给出:

我现有的表结构及其数据
我希望得到的结果.

作者: qianjin036a   发布时间: 2011-12-08

谢谢

现有的标结构为 docid,doctitle
  1, hello
  2, nihao
  3,你好

如果输入参数 docid 的串为 1,3,3,1,2,1
那么返回的结果为:

1, hello
3,你好
3,你好
1, hello
2, nihao
1, hello


   

引用 6 楼 qianjin036a 的回复:

引用 4 楼 chilli6519 的回复:

谢谢,那我怎样样才能得到我希望的结果

重复的数字重复出现呢?

引用 3 楼 qianjin036a 的回复:

in(1,1,5,5)
等价于:
in(1,5)


请给出:

我现有的表结构及其数据
我希望得到的结果.

作者: chilli6519   发布时间: 2011-12-08

SQL code
create table tb(docid int,doctitle nvarchar(10))
insert into tb select 1,'hello'
insert into tb select 2,'nihao'
insert into tb select 3,'你好'
go
declare @docid nvarchar(20)
set @docid='1,3,3,1,2,1'
select a.docid,b.doctitle from(
select substring(@docid,number,charindex(',',@docid+',',number+1)-number) as docid from master..spt_values
where type='p' and number<=len(@docid) and substring(@docid,number,1)<>',' and substring(','+@docid,number,1)=','
)a inner join tb b on a.docid=b.docid
/*
1, hello
3,你好
3,你好
1, hello
2, nihao
1, hello
*/
go
drop table tb

作者: qianjin036a   发布时间: 2011-12-08

SQL code
create table tb(docid int,doctitle nvarchar(10))
insert into tb select 1,'hello'
insert into tb select 2,'nihao'
insert into tb select 3,'你好'
go
declare @docid nvarchar(20)
set @docid='1,3,3,1,2,1'
select a.docid,b.doctitle from(
select substring(@docid,number,charindex(',',@docid+',',number+1)-number) as docid from master..spt_values
where type='p' and number<=len(@docid) and substring(@docid,number,1)<>',' and substring(','+@docid,number,1)=','
)a inner join tb b on a.docid=b.docid
/*
docid                doctitle
-------------------- ----------
1                    hello
3                    你好
3                    你好
1                    hello
2                    nihao
1                    hello

(6 行受影响)

*/
go
drop table tb

作者: qianjin036a   发布时间: 2011-12-08

SQL code
select a.* from 表 a,(select 1 as id union all select 3783
                        union all select 1  union all select 5 union all 
                        select 5 union all select 106 union all select 368 
                        union all select 371 union all select 1376) b
where a.docid =b.id

作者: ssp2009   发布时间: 2011-12-08

测试了你的例子,没有问题,但在我实际的数据库出现问题

传入字符串:1,3783,1,5,5,
得到结果:

1 训练名称:抬头看
1 训练名称:抬头看
3783 手指宝宝—玩弄手指,认识自己身体部位
5 虫虫飞
5 虫虫飞
106 看一看他是谁


1 和 3783反了



引用 9 楼 qianjin036a 的回复:

SQL code
create table tb(docid int,doctitle nvarchar(10))
insert into tb select 1,'hello'
insert into tb select 2,'nihao'
insert into tb select 3,'你好'
go
declare @docid nvarchar(20)
set @docid='1,3,……

作者: chilli6519   发布时间: 2011-12-08

传入的串内容是不确定的,这个写死了,如果串长了,岂不

引用 10 楼 ssp2009 的回复:

SQL code
select a.* from 表 a,(select 1 as id union all select 3783
union all select 1 union all select 5 union all
select 5 union all select 106 uni……

作者: chilli6519   发布时间: 2011-12-08

SQL code
  use tempdb
  go
  --测试数据
  declare @s varchar(1000)
  set @s='ak47,mp5,1,23'
  /*要求输出结果
  S
  ----
  ak47
  mp5
  1
  23
  */
  --3种方法对比:
  --1.[朴实]动态Exec方法:
  declare @s1 varchar(1000)
  set @s1=right(replace(','+@s,',',''' as S union select '''),len(replace(','+@s,',',''' as S union select '''))-12)+''''
  exec(@s1)
  --2.[变通]表交叉方法:
  select replace(reverse((left(s,charindex(',',s)))),',','') as S from(
  select r,reverse(left(@s,r))+',' as s
  from(
  select (select count(*) from sysobjects where name<=t.name ) as r
  from sysobjects t
  )a where r<=len(@s)
  and left(@s+',',r+1) like '%,'
  )t order by r
  --3.[高级]XML方法:
  DECLARE @idoc int;
  DECLARE @doc xml;
  set @doc=cast('<Root><item><S>'+replace(@s,',','</S></item><item><S>')+'</S></item></Root>' as xml)
  EXEC sp_xml_preparedocument @Idoc OUTPUT, @doc
  SELECT * FROM OPENXML (@Idoc, '/Root/item',2)
  WITH (
  [S] varchar(10)
  ) 

作者: fredrickhu   发布时间: 2011-12-08

SQL code
create table tb(docid int,doctitle nvarchar(10))
insert into tb select 1,'hello'
insert into tb select 2,'nihao'
insert into tb select 3,'你好'
insert into tb select 120,'你好aaaa'

declare @str varchar(2000)='1,3,3,120,2,1'
set @str='select a.* from tb a,(select '+REPLACE(@str,',',' as id union all select ')
    +') b where a.docid=b.id'
print @str
exec (@str)

/*
docid       doctitle
----------- ----------
1           hello
3           你好
3           你好
120         你好aaaa
2           nihao
1           hello

作者: ssp2009   发布时间: 2011-12-08