黑马程序员技术交流社区

标题: sql的多条件查询 [打印本页]

作者: 石贤    时间: 2013-11-28 20:29
标题: sql的多条件查询
同事在文本框中输入多条查询条件,没有输入的条件作废。怎么用SQL语句实现
作者: u010209195    时间: 2013-11-28 21:40
使用If else试一下
或者是存储过程,
作者: Sayme    时间: 2013-11-28 22:10
这个跟SQL没有多大关系

主要是你拿到控件的值的时候 你要做下判断

根据判断分支来执行对应的SQL语句
作者: 黑马—严守卫    时间: 2013-11-28 22:27
根据你写的查询条件判断,写出sql语句。再获取你同事选择的值,执行对应的sql语句。
作者: y494890511    时间: 2013-11-28 23:34
  1. 1.声明泛型集合接收sql语句和参数,动态添加参数
  2. List<string> whereList=new List<string>();
  3.             List<SqlParameter> paramsList = new List<SqlParameter>();
  4.             if (cbSearchByName.IsChecked == true)
  5.             {
  6.                 whereList.Add("Name=@Name");
  7.                 paramsList.Add(new SqlParameter("@Name",txtName.Text));


  8.             }
  9.             if (cbSearchByInDate.IsChecked == true)
  10.             {
  11.                 whereList.Add("InDate>=@InDateStart and InDate<=@InDateEnd");
  12.                 paramsList.Add(new SqlParameter("@InDatestart",dpInDateStart.SelectedDate));
  13.                 paramsList.Add(new SqlParameter("@InDateEnd",dpInDateEnd.SelectedDate));
  14.             }
  15.             if (cbSearchByDepartment.IsChecked == true)
  16.             {
  17.                 whereList.Add("DepartmentId=@DepartmentId");
  18.                 paramsList.Add(new SqlParameter("@departmentId",cmbDept.SelectedValue));

  19.             }
  20.             string whereSql = string.Join(" and ",whereList);//给sql语句添加条件
  21.             string sql = "select * from T_Employee";
  22.             if (whereSql.Length > 0)
  23.             {
  24.                 sql = sql + " where " + whereSql;//拼凑sql条件语句
  25.             }
  26.             Employee[] result = new EmployeeDAL().Search(sql, paramsList);//添加参数
  27.             datagrid.ItemsSource = result;


复制代码



作者: 〆、单曲循环    时间: 2013-11-29 13:41
sql = "select * from tablename where 1 = 2"

foreach..//遍历所有输入查询条件
{
      sql += " or [输入查询条件]"//这里如果还有或的逻辑判断一定要注意运算优先级
}


童鞋 如果以上是你想要的请把提问设置为提问结束  方便斑竹美眉加分
作者: 〆、单曲循环    时间: 2013-11-29 13:50
〆、单曲循环 发表于 2013-11-29 13:41
sql = "select * from tablename where 1 = 2"

foreach..//遍历所有输入查询条件

又仔细看了楼主的问题   发现不是我想的酱紫  哎  没办法回答你的问题了   智商是硬伤啊
作者: 茹化肖    时间: 2013-11-29 18:47
sql语句用参数拼接,把文本框值作为参数,没有的参数就是空 这样在sql语句也就不会影响了
作者: Var    时间: 2013-11-29 22:13
拼接查询语句

string sqlStr="SELECT * FROM TableName WHERE  "
if(逻辑表达式)

     sqlStr=sqlStr+"查询条件"

。。。。。

如果拼接次数过多,就使用StringBuilder
作者: 石贤    时间: 2013-11-30 19:57
谢谢。有所领悟了
作者: 红鹰(Jake)    时间: 2013-12-7 12:04
我一般都是通过文本框中的值类型判断条件,然后在向查询语句中添加查询条件,一步一步的拼接而成
作者: 茹化肖    时间: 2013-12-8 19:10
最好的办法就是存储过程、IF esle  也行。以前用过。多条件筛选。
作者: 祐掱緣    时间: 2013-12-12 21:57
用list集合拼接就很好了,给分吧

作者: liyi2013    时间: 2013-12-13 16:58
语句等于1
作者: 鸡蛋壳    时间: 2013-12-16 01:16
SQL Server多条件查询我们经常会用到,下面就教您如何使用存储过程实现SQL Server多条件查询,希望对您学习SQL Server多条件查询方面有所帮助。

/*查询资讯类别列表*/
IF EXISTS (SELECT name FROM sysobjects
         WHERE name = 'get_list_newscate' AND type = 'P')
   DROP PROCEDURE get_list_newscate
GO
create proc get_list_newscate
@newscate_pid int=null,
@newscate_depth int=null,
@newscate_language int=null
as
select * from [news category] where 1=1
and (@newscate_pid is null or newscate_pid=@newscate_pid)
and (@newscate_depth is null or newscate_depth=@newscate_depth)
and (@newscate_language is null or newscate_language=@newscate_language)
order by newscate_orderid

此存储过程可以实现SQL Server多条件查询。

可以用于网站高级检索的功能

查询条件默认为null
程序中值类型不能为null可以在类型后加一个?这样就可以null
比如:int i=null 错误 int? i=null正确

where 1=1是为了当查询条件都不需要用到时就查询全部数据




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2