sql如何查询字段值是条件的一部分

相当之稳重 2012-03-13 09:41:18
如题。。模糊查询 select * from Table where name like '%张%' 只是查询条件是字段值的一部分的吧?

我现在想查询字段值是条件的一部分,比如我的条件集合是 ('12','34','56','78','90') 而我的字段可能是12,可能是34 也可能是 12,34


我原来的SQL写的是 where 字段 in result 其中result 就是我得到的条件集合,但是这样只能查询含有集合中的一个值的,有多个值的字段就不行了。于是想到了模糊查询,但好像又不太对。求赐教啊。。。
...全文
568 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
相当之稳重 2012-03-13
  • 打赏
  • 举报
回复
好吧。。刚刚接触.net 用了linq 忘了 ADO的人伤不起啊。。。刚刚楼主解决问题去了 现在OK了 等我再整理下马上结贴 感谢大家的帮助 好人一生平安。。
chen_ya_ping 2012-03-13
  • 打赏
  • 举报
回复
sql拼接
  • 打赏
  • 举报
回复
or拼接都不肯的人哪~~~~
不过如果多个这样的。。。效率可想
EnForGrass 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用楼主 ptyzhu 的回复:]
如题。。模糊查询 select * from Table where name like '%张%' 只是查询条件是字段值的一部分的吧?

我现在想查询字段值是条件的一部分,比如我的条件集合是 ('12','34','56','78','90') 而我的字段可能是12,可能是34 也可能是 12,34


我原来的SQL写的是 where 字段 in result 其中res……
[/Quote]
首先我不知道我理解对没有
动态设置sql语句,如

DECLARE @sqlvar varchar(8000)
SET @sqlvar='12,34,56,78,90'--你的条件集合
SELECT * FROM Table WHERE CHARINDEX('12,34',@sqlvar)>0--"12,34"就是你的字段值

SomethingJack 2012-03-13
  • 打赏
  • 举报
回复
楼主人呢?
yyl8781697 2012-03-13
  • 打赏
  • 举报
回复
你的字段的值可能同时出现12,34?用where In 试试
explorerqp 2012-03-13
  • 打赏
  • 举报
回复
where in 语句执行太慢,可以考虑使用StringBuffer、拼接sql语句,增加判断:
if(参数 !=null){
sql。append(“and 字段 like ‘参数’”);
}
tptptp00 2012-03-13
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 q107770540 的回复:]

select * from Table where name like '%张%' ||name like '%12%' || name like '%34%'
[/Quote]
linq用多了把 sql可以用||?
SomethingJack 2012-03-13
  • 打赏
  • 举报
回复
难道我误解了- -`
jodgen 2012-03-13
  • 打赏
  • 举报
回复
讲result分成单个的,然后

where 字段=result1 or 字段=result2 or 字段=result3

where部分用for语句来循环得到
yuguojiangnan 2012-03-13
  • 打赏
  • 举报
回复
还是得用where in(select ziduan from table)
q107770540 2012-03-13
  • 打赏
  • 举报
回复
select * from Table where name like '%张%' ||name like '%12%' || name like '%34%'
SomethingJack 2012-03-13
  • 打赏
  • 举报
回复
SQL拼接呀 小喷油
参考..
不知道发了多少次了拉- -
/// <summary>
/// 根据日期,资源名称,资源描述查询资源信息
/// </summary>
/// <returns></returns>
public DataSet FN_SerchByDateAndType(Guid FolderId, NRModel.File model, string createdate, string endate)
{
string strSql = "select * from t_File where 1 =1 and FolderId=@FolderId";
string strWhere = "";
if (!string.IsNullOrEmpty(model.FileNam))
{
strWhere += " and FileNam like @FileNam";
}
//if (!string.IsNullOrEmpty(model.Decription)k)
//{
// strWhere += " and Decription like @Decription";
//}
if (!string.IsNullOrEmpty(createdate) || !string.IsNullOrEmpty(endate))
{
strWhere += " and CreateOn between @createdate and @endate order by ModefyOn desc";
}
strSql += strWhere;
SqlParameter[] parameters = {
new SqlParameter("@FolderId", SqlDbType.UniqueIdentifier),
new SqlParameter("@FileNam", SqlDbType.NVarChar, 256),
new SqlParameter("@createdate", SqlDbType.NVarChar),
new SqlParameter("@endate", SqlDbType.NVarChar)
};
parameters[0].Value = FolderId;
parameters[1].Value = "%" + model.FileNam + "%";
//parameters[1].Value = "%" + model.Decription + "%";
parameters[2].Value = createdate;
parameters[3].Value = endate;
return DbHelperSQL.Query(strSql, parameters);
//SqlParameter[] parameters = new SqlParameter[4];
//parameters[0] = new SqlParameter("@FileNam", model.FileNam);
//parameters[1] = new SqlParameter("@stardate", createdate);
//parameters[2] = new SqlParameter("@enddate", endate);
////执行存储过程
//return DbHelperSQL.RunProcedure("P_UserSerch", parameters, "t_File");
}

这是一个参考代码 可以选一个条件查询 可以选择多个。

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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