SQL中多條件查詢括號的用途


界面:

 

復制代碼
代碼
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 ];
        }
復制代碼

 

 

分類: MSSQL, Asp.net
1
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  星朝  閱讀( 3937)  評論( 0編輯  收藏


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM