这个Sql语句如何写(IN)?

honpher 2009-05-27 01:24:26
假如这么个表table1 :

name mount grade
张三 89 1
李四 78 1
王五 95 2
赵六 86 1

比如说,这个语句:
string strSQL = "select * from table1 where name in ('张三','李四','赵六')";

我要将 in 中的 ('张三','李四','赵六')通过字符串数组赋值,然后放到语句中。

即:
string[] names = new string[3]{"张三","李四","赵六"};

我的问题是:如何将字符串数组赋予sql语句,实现与strSQL 语句同样的查询效果?
...全文
115 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
honpher 2009-05-30
  • 打赏
  • 举报
回复
感谢大家帮助,我已经使用另外方法实现,使用动态字符串数组赋值看来不大行。大家说的方法好像只适用于从字符串中检查某个字符。
jiangshun 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 csdyyr 的回复:]
SQL code
--try:
string[] names = new string[3]{"张三","李四","赵六"};
string strSQL = "select * from table1 where where charindex(','+name+',', ','+names+',')>0";
[/Quote]
数组能行吗?names是字符串才可以啊!估计这个具体会报错
jwdream2008 2009-05-27
  • 打赏
  • 举报
回复
"select  * from table1 where charindex(name," & names & ")>0"
honpher 2009-05-27
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sdhdy 的回复:]
VB code"select * from table1 where charindex(name," & names & ")>0"
[/Quote]



专家,请问C#code怎么写?
sdhdy 2009-05-27
  • 打赏
  • 举报
回复
"select  * from table1 where charindex(name," & names & ")>0"
jiangshun 2009-05-27
  • 打赏
  • 举报
回复



declare @s varchar(8000)
set @s='张三,李四,赵六'
set @s=''''+replace(@s,',',''',''')+''''
select @s

/*

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
'张三','李四','赵六'

(所影响的行数为 1 行)
*/
csdyyr 2009-05-27
  • 打赏
  • 举报
回复

--try:
string[] names = new string[3]{"张三","李四","赵六"};
string strSQL = "select * from table1 where where charindex(','+name+',', ','+names+',')>0";
jwdream2008 2009-05-27
  • 打赏
  • 举报
回复
不懂帮顶!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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