判断几个参数的不同状态,不同的情况写SQL语句判断条件,优化做法(仅供新手)
来源:互联网 发布:模拟投资软件 编辑:程序博客网 时间:2024/06/15 13:32
假如,有 $a,$b,$c这三个变量,复杂的做法是这样的:
//假如三个值都不为空if($a != null && $b != null && $c != null){ $where = "test1 =a and test2 = b and test3 = c ";}//假如$a为空if($a == null && $b != null && $c != null){ $where = "test2 = b and test3 = c";}//假如$b为空if($a != null && $b == null && $c != null){ $where = "test1 = a and test3 = c";}//假如$c为空if($a != null && $b != null && $c == null){ $where = "test1 = a and test2 = b";}//假如$a和$b为空if($a == null && $b == null && $c != null){ $where = "test3 = c";}//假如$a和$c为空if($a == null && $b != null && $c == null){ $where = "test2 = b";}//假如$b和$c为空if($a != null && $b == null && $c == null){ $where = "test1 = a";}//假如都为空if($a == null && $b == null && $c == null){ $where = "";}
这样的代码量比较大,而且写起来非常乱,很容易就把变量搞错了。这时候就需要优化一下我们的代码。如下:
$where = "1=1";if($a != null){ $where .=" and test1 = a ";}if($b != null){ $where .= " and test2 = b ";}if($c != null){ $where .= " and test3 = c ";}//这个时候,就可以把$where打印出来看看echo $where;
一定要注意,SQL语句直接的空格,千万别漏了!
0 0
- 判断几个参数的不同状态,不同的情况写SQL语句判断条件,优化做法(仅供新手)
- 重复执行sql语句前的判断参考-仅供自己备忘参考
- 根据条件判断使用不同的class
- SQL中不同的参数使用不同的条件
- sql查询空值判断不同数据库的不同写法
- Oracle 的sql语句多条件判断
- 在同一个sql语句中写不同条件的count数量,和查询半年的数据
- sql语句中如何根据不同的条件,执行不同的where条件
- C语句中涉及到条件判断时的情况分析
- 判断sim卡的状态进行不同的设定
- POJ 3449 Geometric Shapes(判断几个不同图形的相交)
- 枚举 判断给定数列中 有几个不同的数
- SQL条件判断语句
- SQL条件判断语句
- 上下班的判断(不同季度不同时间段)
- 写一个函数,判断不同模式处理器的返回值
- 关于非线性优化fminbnd函数的说明(仅供新手参考)【转】
- Oracle与SQL Server几个语句的不同
- hashmap的key值不能重复
- Find Minimum in Rotated Sorted Array
- android 自定义view的使用(最佳demo——返回标题栏)
- 使用std::move()提醒编译器对返回值进行优化
- 正益移动:AppCan核心竞争力是开放的生态系统
- 判断几个参数的不同状态,不同的情况写SQL语句判断条件,优化做法(仅供新手)
- git一些操作命令
- 程序猿必知的swift单例编写的正确方式
- iOS定位添加大头针
- 加载系统相册中图片错误CGBitmapContextCreate: unsupported color space.
- A. Uncowed Forces
- 自定义Dialog和DialogFragment
- core animation初识之CALayer(二)
- 一看就懂的ReactJs入门教程(精华版)