SQL 多條件查詢
以後我們做多條件查詢,一種是排列結合,另一種是動態拼接SQL
如:我們要有兩個條件,一個日期@addDate,一個是@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
第二種就是動態組成SQL,通過exec來執行,我就不寫,
昨天我想到一種辦法
select * from table where (addDate = @addDate or @addDate is null) and (name = @name or @name = '')
結果一調試,成功,
一點想法,有更好方法的,請指教!~
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!