where id in (...)

小驴来这里学习 2012-02-17 09:45:05
我想做一个查询,可以查询多条记录。
类似于这样:
select * from tblA where id in ('00001','00003','00008')

我给用户了一个文本框,并给了用户在输入的时候,用逗号把内容分开。
然后,我发现问题不是我想的那么容易。
在我看来,sql语句会变成这样:
select * from tblA where id in ('00001,00003,00008')


不知道我描述的是否清楚,如果需要,我会再补充。
劳烦各位帮帮忙。
...全文
455 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
AcHerat 2012-02-17
  • 打赏
  • 举报
回复

--select * from tblA where id in ('00001,00003,00008')


select * from tblA where charindex(','+ltrim(id)+',',','+'00001,00003,00008'+',')>0
Felixzhaowenzhong 2012-02-17
  • 打赏
  • 举报
回复
SELECT ''''+REPLACE('1,2,3,5,6',',',''',''')+''''
--结果为
'1','2','3','5','6'

用这种类似方法替换试一下
dawugui 2012-02-17
  • 打赏
  • 举报
回复
select * from tblA where id in ('00001','00003','00008')

-->

select * from tblA where charindex(id ,'00001,00003,00008') > 0

select * from tblA where charindex(id ,你的字符串变量) > 0
ILOVE_ASPNET 2012-02-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 hj_daxian 的回复:]

这样不行吗 将,替换成',' 转义失败?
SQL code
where id in (replace(@id,',',''',''')
[/Quote]

这要是不行的话,那就只能用绝招了 textStr.Split(',') 分割三 数组, 然后循环 用字符串拼接起来
string text = "00001,00003,00008";
string[] array = text.Split(',');
string str = string.Empty;
for (int i = 0; i < array.Length; i++)
{
str += "'" + array[i].ToString() + "',";
}
str.Substring(0, str.Length - 1);
这样的话,那么就把str拼上去了。
  • 打赏
  • 举报
回复
这样不行吗 将,替换成',' 转义失败?
where id in (replace(@id,',',''',''')




  • 打赏
  • 举报
回复
[Quote=引用 7 楼 hj_daxian 的回复:]
貌似这个最简单 在程序里面直接替换就可以了 间接思维
[/Quote]
是啊,但问题是,程序不是我写的,你懂的。
他的做法是,gridview+objectdatasource+xx.xsd(数据集)。
整个儿都不是用写代码的方法实现的。
  • 打赏
  • 举报
回复

--用临时表作为数组
create function f_split(@c varchar(2000),@split varchar(2))
returns @t table(col varchar(20))
as
begin

while(charindex(@split,@c)<>0)
begin
insert @t(col) values (substring(@c,1,charindex(@split,@c)-1))
set @c = stuff(@c,1,charindex(@split,@c),'')
end
insert @t(col) values (@c)
return
end
go

select * from dbo.f_split('dfkd,dfdkdf,dfdkf,dffjk',',')

drop function f_split
col
--------------------
dfkd
dfdkdf
dfdkf
dffjk

(所影响的行数为 4 行)
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 maco_wang 的回复:]

替换, 变成','
[/Quote]

貌似这个最简单 在程序里面直接替换就可以了 间接思维
  • 打赏
  • 举报
回复

多谢各位大牛。
我补充一下,之前的查询不是我写的,很大一坨,而且是在.xsd数据集里面。
我对SQL的了解程度仅仅是简单的增删改查,我不想去改之前的那一大坨,你懂的。
根据2楼的,我把查询改成了这样
where  id in (''' + replace(@id,',',''',''')

同之前一样,当我只输入一个,比如 00001的时候,是可以的,但是输入 00001,0003 就什么都查不到。

4楼的,我看不太懂。但是在我这里写
declare @s1 varchar(1000)

是不行的。

之前没讲清楚,抱歉啊。
叶子 2012-02-17
  • 打赏
  • 举报
回复
替换, 变成','
--小F-- 2012-02-17
  • 打赏
  • 举报
回复
  declare @s1 varchar(1000)
  set @s1=right(replace(','+@s,',',''' as S union select '''),len(replace(','+@s,',',''' as S union select '''))-12)+''''
  exec(@s1)
--小F-- 2012-02-17
  • 打赏
  • 举报
回复
你的意思就是动态拆分呗。
老猫五号 2012-02-17
  • 打赏
  • 举报
回复
declare @v nvarchar(2000)
set @v = '00001,00003,00008';
set @v = ''' + replace(@v,',',''',''') + ''';
select * from tblA where id in (@v);
喜阳阳 2012-02-17
  • 打赏
  • 举报
回复
strID = "\'" + strID.Trim().Replace(",", "\',\'") + "\'";
处理下字符串。
  • 打赏
  • 举报
回复
我决定不做了,谢谢各位帮忙。
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 dawugui 的回复:]

select * from tblA where id in ('00001','00003','00008')

-->

select * from tblA where charindex(id ,'00001,00003,00008') > 0

select * from tblA where charindex(id ,你的字符串变量) > 0
[/Quote]
上面是用了这个方法。


另外14楼的会告诉我语法错误。
  • 打赏
  • 举报
回复
。。。。



gengchenhui 2012-02-17
  • 打赏
  • 举报
回复
你把你的,替换成','不就行了?

22,207

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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