SQL模糊查询问题
string strSQL = @"select * from 表1 where 1=1";if (this.txtGatePassNo.Text.Trim() != "")
strSQL += " and GATEPASS_NO
this.txtGatePassNo.Text.Trim()是要输入的查询条件 ,格式是 a,b,c这种以逗号隔开的字符串 ,GATEPASS_NO是查询条件要匹配的字段
该如何实现呢? 如果是单个查询a 很好写啊 GATEPASS_NO like '%" + this.txtCAR_NO.Text.Trim() + "%'";
关键是现在this.txtGatePassNo.Text.Trim()输入的值可以是 a,b,c,d这种多个数据 该要如何实现呢
麻烦前辈们抽空解决!拜谢 --------------------编程问答-------------------- 你循环一下 txtGatePassNo 然后拼一个 sql语句出来 不行吗 --------------------编程问答--------------------
能麻烦你一下详细点么 大四学生实习中很多不懂的啊 T.T --------------------编程问答--------------------
--------------------编程问答-------------------- a,b,c,d分别代表什么。。。
foreach(string s1 in txtGatePassNo.Text.Trim().Split(','))
{
strSQL += " and GATEPASS_NO like '%"+s1+"%' ";
}
--------------------编程问答-------------------- 这个意思?or 还是and --------------------编程问答--------------------
大四了这点逻辑都没有么? --------------------编程问答--------------------
or --------------------编程问答--------------------
不行的 虽然拆分了查询条件 但是s1的内容等于是一个了 如果我要查询 a,b,c 你这样变成查abc了.... --------------------编程问答--------------------
学习网络的,现在在实习让我改.net东西 我真的不会 如果你会 麻烦帮忙解决 --------------------编程问答--------------------
输入的查询内容,比如 xiao,da,san 这些 也就是说查询出的内容是包含 xiao da san这些字段的内容 --------------------编程问答--------------------
输入的时候以逗号分割‘,’,然后你在后台截取字符串不就出来了么。。
试试。。 --------------------编程问答--------------------
不好意思说话可能难听了,其实上面高人已经有解决方案了,参考下,结合自身需要把,这样也能锻炼你的思维啊,是不? --------------------编程问答-------------------- 循环,字符串分割,拼串。 --------------------编程问答-------------------- 用逗号隔开 循环叠加查询语句 --------------------编程问答-------------------- DECLARE @idlist varchar(100)
SET @idlist='1,2,3'
--查询
SELECT * FROM tbname WHERE CHARINDEX(','+RTRIM(fdname)+',',','+@idlist+',')>0
SELECT * FROM tbname WHERE PATINDEX('%,'+RTRIM(fdname)+',%',','+@idlist+',')>0
SELECT * FROM tbname WHERE ','+@idlist+',' LIKE '%,'+RTRIM(fdname)+',%'
GO --------------------编程问答--------------------
select * from table where charindex(','+@fname+',',','+Expand+',')>0--------------------编程问答--------------------
如果把1,2,3换成txtGatePassNo.Text.Trim()该怎么写呢 求教 --------------------编程问答-------------------- string[] str = txtGatePassNo.Text.Trim().Split(',');
foreach(string s1 in str){
strSQL += " and GATEPASS_NO like '%"+s1+"%' ";
}
--------------------编程问答-------------------- string strSQL = @"select * from 表1 where 1=1";
if (this.txtGatePassNo.Text.Trim() != "")
strSQL += " and '"+this.txtGatePassNo.Text.Trim()+"' LIKE '%'+GATEPASS_NO+',%'" --------------------编程问答-------------------- for (int i = 0; i < arrGatePassNo.Length; i++)
{
if (i == 0)
{
strCon = " and (GatePass_No like '%" + arrGatePassNo[i] + "%' ";
if (i + 1 == arrGatePassNo.Length)
{
strCon += ")";
}
}
else if (i < arrGatePassNo.Length - 1)
strCon += " or GatePass_No like '%" + arrGatePassNo[i] + "%' ";
else
strCon += " or GatePass_No like '%" + arrGatePassNo[i] + "%')";
}
if (GatePassNo != "")
{
strSQL += strCon;
} --------------------编程问答-------------------- 拆分字符串之后要用or吧! --------------------编程问答-------------------- 没人说对的 我贴个正确的吧 是公司项目经理帮我弄的
for (int i = 0; i < arrGatePassNo.Length; i++)
{
if (i == 0)
{
strCon = " and (GatePass_No like '%" + arrGatePassNo[i] + "%' ";
if (i + 1 == arrGatePassNo.Length)
{
strCon += ")";
}
}
else if (i < arrGatePassNo.Length - 1)
strCon += " or GatePass_No like '%" + arrGatePassNo[i] + "%' ";
else
strCon += " or GatePass_No like '%" + arrGatePassNo[i] + "%')";
}
if (GatePassNo != "")
{
strSQL += strCon;
} --------------------编程问答--------------------
这个就可以满足楼主的要求了啊? 有什么不对吗? --------------------编程问答-------------------- foreach(string s1 in txtGatePassNo.Text.Trim().Split(','))
{
strSQL += " and GATEPASS_NO like '%"+s1+"%' ";
}
比如你输入a,b,c
--------------------编程问答--------------------
and GATEPASS_NO like '%"+a+"%' and GATEPASS_NO like '%"+b+"%' and GATEPASS_NO like '%"+c+"%'";
支持 字符串分割。。。拼接 --------------------编程问答-------------------- 也可以定义一个参数来些会简单点。string paramCode=this.txtGatePassNo.Text.Trim();
然后用参数就行了
--------------------编程问答-------------------- 学习一下 --------------------编程问答-------------------- for (int i = 0; i < arrGatePassNo.Length; i++)
{
if (i == 0)
{
strCon = " and (GatePass_No like '%" + arrGatePassNo[i] + "%' ";
if (i + 1 == arrGatePassNo.Length)
{
strCon += ")";
}
}
else if (i < arrGatePassNo.Length - 1)
strCon += " or GatePass_No like '%" + arrGatePassNo[i] + "%' ";
else
strCon += " or GatePass_No like '%" + arrGatePassNo[i] + "%')";
}
if (GatePassNo != "")
{
strSQL += strCon;
} --------------------编程问答-------------------- 上面的都说的很好了。。。。。兄弟你可以结贴了。。 --------------------编程问答-------------------- --------------------编程问答-------------------- 哎,我凑,前段时间客户也要求做类似这样的查询,想来想去,除了盘古分词,就是这种最原始的了:
多个条件组合,输入查询条件以逗号隔开,可快速定位信息:
--------------------编程问答-------------------- foreach(string s1 in txtGatePassNo.Text.Trim().Split(','))
string str_Where ="";
string strWhe = 文本框的值;
string[] aaa = strWhe.Split(',');//以逗号拆分
if (aaa.Length > 0)
{
for (int i = 0; i < aaa.Length; i++)
{
//拼接
if (str_Where == "")
str_Where = " _Name like '%" + aaa[i].ToString() + "%'";
else
str_Where += " and _Name like '%" + aaa[i].ToString() + "%'";
}
}
{
strSQL += " or GATEPASS_NO like '%"+s1+"%' ";
}
如果txtGatePassNo输入1,2,3
就是查询的数据只要包涵1或者2或者3的都会查询出来,应该是OR 不是AND
如果是AND的话,查询出来的是既包含1同时包含2同时包含3的数据。
--------------------编程问答-------------------- 不知楼主想要的是GATEPASS_NO LIKE '%a%b%c%'
还是 (GATEPASS_NO LIKE '%a%' OR GATEPASS_NO LIKE '%b%' OR GATEPASS_NO LIKE '%c%') --------------------编程问答-------------------- 拆分,拼串
foreach(string s1 in txtGatePassNo.Text.Trim().Split(','))
{
strSQL += " or GATEPASS_NO like '%"+s1+"%' ";
}
完成!
补充:.NET技术 , ASP.NET