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

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

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

标签:SQL,多条件,查询
0
投稿

猜你喜欢

  • Mysql教程分组排名实现示例详解

    2024-01-18 14:19:49
  • JDBC-idea导入mysql连接java的jar包(mac)的方法

    2024-01-22 22:51:08
  • 三表左连接查询的sql语句写法

    2024-01-15 01:54:21
  • Python入门教程(二)Python快速上手

    2023-10-16 08:54:09
  • Python制作微信机器人教程详解

    2021-05-09 13:44:44
  • PHP求最大子序列和的算法实现

    2023-11-15 04:17:28
  • python中如何写类

    2023-09-02 18:19:58
  • MySQL数据库表被锁、解锁以及删除事务详解

    2024-01-24 14:17:51
  • 解决Django数据库makemigrations有变化但是migrate时未变动问题

    2024-01-15 16:55:39
  • Python *args和**kwargs用法实例解析

    2023-01-16 18:30:55
  • 通过python下载FTP上的文件夹的实现代码

    2022-03-16 11:19:12
  • 请谨慎对待程序的图标和名称

    2011-06-16 20:35:22
  • golang时间/时间戳的获取与转换实例代码

    2023-09-02 06:04:43
  • 用实例详解Python中的Django框架中prefetch_related()函数对数据库查询的优化

    2024-01-19 08:47:12
  • python快排算法详解

    2023-08-24 04:17:08
  • pytorch实现onehot编码转为普通label标签

    2023-04-18 10:19:21
  • 详解用Python爬虫获取百度企业信用中企业基本信息

    2022-07-27 18:49:52
  • 如何用Python徒手写线性回归

    2023-06-12 13:47:14
  • 设计输入了些什么?

    2008-04-02 11:16:00
  • Vue自定义指令实现弹窗拖拽四边拉伸及对角线拉伸效果

    2024-05-28 15:43:08
  • asp之家 网络编程 m.aspxhome.com