界面:

代码

select id,routeName,routeCharacteristic,routeIntroductions,costDetail,participate,click,routeCategory,dineMenu,weather,isEnable,addPerson,addDate,competitiveProducts,luxury,onVacation,characteristic,hotRecommend,referencesPrice,specialPreference,imgShow,imgName,imgUrl,newProduct,overflow,season,priority from Tab_TouristTrack
where (addDate>=@beginDate or @beginDate='') and (addDate<=@endDate or @endDate='')
and (routeName like @routeName or @routeName='')
and (newProduct=1 or competitiveProducts=1 or luxury=1 or onVacation=1 or characteristic=1
or specialPreference=1 or hotRecommend=1 or overflow=1 or season=1)
and (priority=0 or priority is null)

以上语句是一个多条件查询语句,假如没有用括号分开的话,那么只有所有条件都为真才会查出数据库中所满足条件的行,但是这不是我所想要的,所以才需要用括号如:(addDate>=@beginDate or @beginDate=''),意思是当用户输入数据时,则根据addDate>=@beginDate的条件来查询数据库中满足条件的数据,如果用户不输入任何数据,那么这个时候文本框所得到的值则为:""-->空的字符串,使用@beginDate='' (变成sql语句:' '=' ' )也能使这个where子句满足条件,否则的话很难实现像这样的多条件语句查询.

在来看下这条子句:

(newProduct=1 or competitiveProducts=1 or luxury=1 or onVacation=1 or characteristic=1
or specialPreference=1 or hotRecommend=1 or overflow=1 or season=1)

这里加括号的意思是当满足前面子句所有条件的情况下并且还要满足括号内这些字段至少有一个为1的数据.

(priority=0 or priority is null)

这里加括号的意思是当满足前面子句所有条件的情况下并且还要满足括号priority=0或者priority为空的数据

如果在子句:

(priority=0 or priority is null)

中不加括号的话,那么priority前满足所有的条件下,在使用or priority is null这样就不是我们要的数据了.

这条子句加括号也是和上面子句同一个意思

(newProduct=1 or competitiveProducts=1 or luxury=1 or onVacation=1 or characteristic=1
or specialPreference=1 or hotRecommend=1 or overflow=1 or season=1)

使用括号的目的就是将一小段sql子句作为一个整体来使用.

简单的说就是在满足前面所有子句的情况下还要满足(priority=0 or priority is null)返回为ture的数据.

得到文本中输入的值调用后台数据库代码

  private void PriorityBinds()
    {
        gvPriority.DataKeyNames = new String[] { "id" };
        gvPriority.DataSource = tts.PriorityQuery(ddlType.SelectedValue,txtBeginDate.Text,txtEndDate.Text
            ,txtTouristTrackName.Text,rblPriority.SelectedValue);
        gvPriority.DataBind();
    }

代码

 public DataTable PriorityQuery(String MenuType, String beginDate, String endDate, String routeName, String priority)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" select id,routeName,routeCharacteristic,routeIntroductions,costDetail,participate,click,routeCategory,dineMenu,weather,isEnable,addPerson,addDate,competitiveProducts,luxury,onVacation,characteristic,hotRecommend,referencesPrice,specialPreference,imgShow,imgName,imgUrl,newProduct,overflow,season,priority ");
            strSql.Append(" from Tab_TouristTrack ");
            strSql.Append(" where (addDate>=@beginDate or @beginDate='') and (addDate<=dateadd(dd,1,@endDate) or @endDate='')");
            strSql.Append(" and (routeName like @routeName or @routeName='')");

            if (MenuType != "all")
            {
                    strSql.Append(" and "+MenuType + "=1");
            }
            else
            {
                strSql.Append(" and (newProduct=1 or competitiveProducts=1 or luxury=1 or onVacation=1 or characteristic=1");
                strSql.Append(" or specialPreference=1 or hotRecommend=1 or overflow=1 or season=1)");
            }

            if (priority!="all")
            {
                if (priority=="1")
                {
                    strSql.Append(" and priority=1");
                }
                else
                {
                    strSql.Append(" and (priority=0 or priority is null)");
                }
            }

            strSql.Append(" order by priority desc");

            SqlParameter[] param = new SqlParameter[] 
            { 
                new SqlParameter("@beginDate",beginDate),
                new SqlParameter("@endDate",endDate),
                new SqlParameter("@routeName","%"+routeName+"%")
            };

            return SQLLinkDatabase.Query(strSql.ToString(),param).Tables[0];
        }
	</div>
<div class="postDesc">posted @ <span id="post-date">2009-12-15 13:28</span> <a href="http://www.cnblogs.com/jhxk/">唔愛吃蘋果</a> 阅读(<span id="post_view_count">8640</span>) 评论(<span id="post_comment_count">0</span>) &nbsp;<a href="https://i.cnblogs.com/EditArticles.aspx?postid=1624705" rel="nofollow">编辑</a> <a href="#" onclick="AddToWz(1624705);return false;">收藏</a></div>
</div>
<script type="text/javascript">var allowComments=true,cb_blogId=56600,cb_entryId=1624705,cb_blogApp=currentBlogApp,cb_blogUserGuid='7c6d27e0-7e46-de11-9510-001cf0cd104b',cb_entryCreatedDate='2009/12/15 13:28:00';loadViewCount(cb_entryId);</script>

posted @
2017-06-05 12:17 
星朝 
阅读(...) 
评论(...) 
编辑 
收藏

SQL中多条件查询括号的用途的更多相关文章

  1. C# 将Access中时间段条件查询的数据添加到ListView中

    C# 将Access中时间段条件查询的数据添加到ListView中 一.让ListView控件显示表头的方法 在窗体中添加ListView 空间,其属性中设置:View属性设置为:Detail,Col ...

  2. SQL Server多条件查询的实现

    SQL Server多条件查询的实现 SQL Server多条件查询我们经常会用到,下面就教您如何使用存储过程实现SQL Server多条件查询,希望对您学习SQL Server多条件查询方面有所帮助 ...

  3. Hibernate中的条件查询完成类

    Hibernate中的条件查询有以下三个类完成: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类

  4. SSM-MyBatis-13:Mybatis中多条件查询

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 实体类 public class Book { private Integer bookID; private ...

  5. ormlite 在android中 排序 条件查询

    ormlite 在android中 排序 条件查询 all = dao.queryBuilder().orderBy("Id", true).where().eq("Ty ...

  6. Hibernate中的条件查询完毕类

    Hibernate中的条件查询有下面三个类完毕: 1.Criteria:代表一次查询 2.Criterion:代表一个查询条件 3.Restrictions:产生查询条件的工具类

  7. C#中使用Sql对Excel条件查询

    如何在C#中实现对Excel的条件查询呢? 在使用Sql条件语句对Excel进行查询时,遇到"至少一个参数没有被指定值"的问题,如何解决? 使用OleDbConnection对象创 ...

  8. MyBatis中的条件查询(动态sql)

    本文将介绍使用MyBatis框架,编写DAO层接口类和接口类对应的sql映射文件,使用动态sql查询满足条件的用户集合. 首先,需要创建一个实体类User,供封装数据使用: package com.x ...

  9. SQL中join连接查询时条件放在on后与where后的区别

    数据库在通过连接两张或多张表来返回记录时,都会生成一张中间的临时表,然后再将这张临时表返回给用户. 在使用left jion时,on和where条件的区别如下: 1. on条件是在生成临时表时使用的条 ...

随机推荐

  1. 由max_allowed_packet引发的mysql攻防大战

    1.原因 程序的sql语句比较长.max_allowed_packet默认是1024.于是就报错了.一开始手动改 global max_allowed_packet ,改完后.莫名奇妙被还原.后来改配 ...

  2. 游戏里的动态阴影-ShadowMap实现原理

    ShadowMap是比较流行的实时阴影实现方案,原理比较简单,但真正实现起来还是会遇到很多问题的,我这里主要记录下实现方式 先看效果 凹凸地形上也有阴影 实现原理 ShadowMap技术是从灯光空间用 ...

  3. SSM整合学习笔记

    对学习Spring+Spring MVC+Mybatis过程中出现的问题解决办法汇总 出现的问题 1.pom.xml报错 右键项目->maven->update project 因此每次更 ...

  4. 关于javacc的认识

    http://www.cnblogs.com/Gavin_Liu/archive/2009/03/07/1405029.html

  5. vue使用之剑走偏锋——菜单组件篇

    背景 习惯了这样的写法了,上来先来一个背景,交待一下事情的起因.事情的起因很简单,用惯了mvc框架,想来一发前后端完全分离的框架试试.选用的人员和技术是这样子的,让原本做java的web开发的人员使用 ...

  6. Apache Kafka - 介绍

    原文地址地址: http://blogxinxiucan.sh1.newtouch.com/2017/07/12/Apache-Kafka-介绍/ Apache Kafka教程 之 Apache Ka ...

  7. Oracle 11g RAC 修改各类IP地址

    Oracle 11g RAC 修改各类IP地址 首先,我们都知道Oracle 11g RAC中的IP主要有:Public IP.VIP.SCAN VIP.Private IP这几种. 一般这类改IP地 ...

  8. 设计模式-策略模式Strategy以及消灭if else

    概述 如果在开发过程中,出现大量的if else或者switch case 语句,如果这些语句块中的代码并不是包含业务逻辑,只是单纯的分流方法,那么,每一个语句块中都是一个算法或者叫策略. 背景 比如 ...

  9. 花了一年时间开发的TTF2FNT字库转换软件

    TTF(True Type Font)字库是微软定义的基于windows的标准字库格式.但其由于专利保护以及无法跨平台导致TTF字库在实际应用中无法有效使用. 为此我开发了TTF2FNT字库转换软件, ...

  10. wordpress设置“固定链接”后,页面404错误的解决方法

    Nginx 解决方案: 网上盛传的方法是: 在 /etc/nginx/nginx.conf文件的 loction / {} 中添加 if (-f $request_filename/index.htm ...