请教关于sql server检索中 in 使用时重复的id也必须检索
时间:2011-12-08
来源:互联网
使用 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 操作的含义.
作者: qianjin036a 发布时间: 2011-12-08
作者: chuanzhang5687 发布时间: 2011-12-08
等价于:
in(1,5)
作者: qianjin036a 发布时间: 2011-12-08
重复的数字重复出现呢?
in(1,1,5,5)
等价于:
in(1,5)
作者: chilli6519 发布时间: 2011-12-08
作者: fuxiyang 发布时间: 2011-12-08
谢谢,那我怎样样才能得到我希望的结果
重复的数字重复出现呢?
引用 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
引用 4 楼 chilli6519 的回复:
谢谢,那我怎样样才能得到我希望的结果
重复的数字重复出现呢?
引用 3 楼 qianjin036a 的回复:
in(1,1,5,5)
等价于:
in(1,5)
请给出:
我现有的表结构及其数据
我希望得到的结果.
作者: chilli6519 发布时间: 2011-12-08
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
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
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反了
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
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
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
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
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28