模糊查询简单分析,得出sql语句
2008-09-17 18:42
731 查看
最近搞了一个登记系统,到了查询页面时候,有多的DropDownList控件,DropDownList里面很多选择可以选择,就是条件查询.但如何获得该查询的sql语句呢,我本来想到用 case,但考虑到与以前查询具体分数不同,它这里是有模糊查询的概念,譬如,当DropDownList里面有"全部"一项时,那么,case要分很细小,还是不妙啊,真实情况是
DropDownList3里面有 选择项 (全部,1,2,3,4,5) DropDownList4里面有选择项(全部,a,b,c,d,e)那么,
考虑下,其实是数学的组合问题,简单演示在草稿上,如果两个DropDownList,则只有四种可能,要么是"全部"要么不是"全部"所以有以下结果
string sql;
sql = "select * from file where 1=1";//一开始要赋给它,否则,编译时候不通过
if(DropDownList3 .SelectedValue =="全部" && DropDownList4 .SelectedValue =="全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'";
if (DropDownList3.SelectedValue == "全部" && DropDownList4.SelectedValue != "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and grade ='" + DropDownList4.SelectedValue + "'";
if (DropDownList3.SelectedValue != "全部" && DropDownList4.SelectedValue == "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and person_shenfen ='" + DropDownList3.SelectedValue + "'";
if (DropDownList3.SelectedValue != "全部" && DropDownList4.SelectedValue != "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and person_shenfen ='" + DropDownList3.SelectedValue + "'and grade ='" + DropDownList4.SelectedValue + "'";
这样就实现了sql语句的查询了.
如果有什么好的建议和意见,欢迎批评指正,谢谢.
DropDownList3里面有 选择项 (全部,1,2,3,4,5) DropDownList4里面有选择项(全部,a,b,c,d,e)那么,
考虑下,其实是数学的组合问题,简单演示在草稿上,如果两个DropDownList,则只有四种可能,要么是"全部"要么不是"全部"所以有以下结果
string sql;
sql = "select * from file where 1=1";//一开始要赋给它,否则,编译时候不通过
if(DropDownList3 .SelectedValue =="全部" && DropDownList4 .SelectedValue =="全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'";
if (DropDownList3.SelectedValue == "全部" && DropDownList4.SelectedValue != "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and grade ='" + DropDownList4.SelectedValue + "'";
if (DropDownList3.SelectedValue != "全部" && DropDownList4.SelectedValue == "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and person_shenfen ='" + DropDownList3.SelectedValue + "'";
if (DropDownList3.SelectedValue != "全部" && DropDownList4.SelectedValue != "全部")
sql = "select * from file where date_year='" + DropDownList1.SelectedValue + "'and subject ='" + DropDownList2.SelectedValue + "'and person_shenfen ='" + DropDownList3.SelectedValue + "'and grade ='" + DropDownList4.SelectedValue + "'";
这样就实现了sql语句的查询了.
如果有什么好的建议和意见,欢迎批评指正,谢谢.
相关文章推荐
- php中简单模糊查询的sql语句
- 几种常用数据库 分页查询的sql语句和简单分析
- SQL Server之 (二) SQL语句 模糊查询 空值处理 聚合函数
- sql语句带参数的模糊查询 (like @tag+'%')
- 收藏一个简单sql语句实现多查询的方法
- PB数据窗口sql语句中like后面怎么跟变量模糊查询
- sql语句查询过慢的原因分析
- SQL语句的模糊查询
- SQL查询语句简要使用精华——之 简单查询
- SQL模糊查询语句和Escape转义字符
- 预声明中的模糊查询sql语句(PreparedStatement like sql)怎么写
- sql条件查询语句的简单实例
- MySQL 使用explain分析sql语句的查询效率
- 基于数据字典的通用查询系统(一)SQL语句的构成分析
- SQL模糊查询语句(like)
- sQL分页查询语句分析
- 再次推荐SQLPrompt3 -简单破解无限期的使用这款很不错的SQL查询分析工具
- C# 简单连接数据库并执行SQL查询语句
- 一句简单的SQL查询语句的背后...
- SQL模糊查询语句和Escape转义字符