网络编程
位置:首页>> 网络编程>> 数据库>> SQL 多条件查询几种实现方法详细介绍

SQL 多条件查询几种实现方法详细介绍

作者:lqh  发布时间:2024-01-16 23:12:31 

标签:SQL,多条件,查询

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 = '')

结果一调试,成功,

一点想法,有更好方法的,请指教!~

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

来源:http://www.cnblogs.com/edobnet/archive/2004/07/16/24781.html

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com