SQL 多條件查詢幾種實(shí)現(xiàn)方法詳細(xì)介紹
SQL 多條件查詢
以后我們做多條件查詢,一種是排列結(jié)合,另一種是動(dòng)態(tài)拼接SQL
如:我們要有兩個(gè)條件,一個(gè)日期@addDate,一個(gè)是@name
第一種寫法是
if (@addDate is not null) and (@name <> '') select * from table where addDate = @addDate and name = @name else if (@addDate is not null) and (@name ='') select * from table where addDate = @addDate else if(@addDate is null) and (@name <> '') select * from table where and name = @name else if(@addDate is null) and (@name = '') select * from table
第二種就是動(dòng)態(tài)組成SQL,通過exec來執(zhí)行,我就不寫,
昨天我想到一種辦法
select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')
結(jié)果一調(diào)試,成功,
一點(diǎn)想法,有更好方法的,請(qǐng)指教!~
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
數(shù)據(jù)庫觸發(fā)器DB2和SqlServer有哪些區(qū)別
大部分?jǐn)?shù)據(jù)庫語句的基本語法是相同的,但具體到的每一種數(shù)據(jù)庫,又有些不一樣,例如觸發(fā)器,DB2和SQL Server兩種很大的不同。對(duì)數(shù)據(jù)庫觸發(fā)器DB2和SqlServer有哪些區(qū)別感興趣的朋友一起看看本文吧2015-11-11Select count(*)、Count(1)和Count(列)的區(qū)別及執(zhí)行方式
這篇文章主要介紹了Select count(*)、Count(1)和Count(列)的區(qū)別及執(zhí)行方式,很多人其實(shí)對(duì)這三者之間是區(qū)分不清的,本文會(huì)闡述這三者的作用、關(guān)系以及背后的原理,需要的朋友可以參考下2015-02-02SQL Server數(shù)據(jù)庫的三種恢復(fù)模式:簡(jiǎn)單恢復(fù)模式、完整恢復(fù)模式和大容量日志恢復(fù)模式
這篇文章主要介紹了SQL Server數(shù)據(jù)庫的三種恢復(fù)模式:簡(jiǎn)單恢復(fù)模式、完整恢復(fù)模式和大容量日志恢復(fù)模式,需要的朋友可以參考下2018-10-10通過系統(tǒng)數(shù)據(jù)庫獲取用戶所有數(shù)據(jù)庫中的視圖、表、存儲(chǔ)過程
本文主要講了通過系統(tǒng)數(shù)據(jù)庫獲取用戶所有數(shù)據(jù)庫中的視圖、表、存儲(chǔ)過程的方法,大家參考使用吧2014-04-04