关于SQL中的 where 1 = 1 的用法
在项目中的常见的一个操作:在有关SQL的代码中加入where 1 = 1,关于它的用法,可以总结如下:
首先,where 1 = 1的用法往往是为了方便后续的给SQL增加where限制条件。如果实现加入了where 1 = 1,后续的条件只需加入and ... 这种形式的代码就可以准确执行了。举个例子,
不加where 1 = 1 时:
sqlStatement = "select * from table_car"
whereClause = "";
if (modelYear != 0)
{
if (whereClause != "")
whereClause = whereClause + " and ";
whereClause += "year="+modelYear;
}
if (manufacturer != "")
{
if (whereClause != "")
whereClause = whereClause + " and ";
whereClause += "value="+manufacturer
}
if (color != "")
{
if (whereClause != "")
whereClause = whereClause + " and ";
whereClause += "color="+color
}
if (california)
{
if (whereClause != "")
whereClause = whereClause + " and ";
whereClause += "hasCatalytic=1"
} if (whereClause != "")
sqlStatement = sqlStatement + "WHERE "+whereClause;
加上where 1 = 1 后:
sqlStatement = "select * from table_car where 1 = 1";
if(Year != 0)
sqlStatement +=" and year=" + modelYear
if(manufacturer != "")
sqlStatement += " and value=" + manufacturer
if(color != "")
sqlStatement += " and color=" + color
if(california != 0)
sqlStatement += " and hasCatalytic=1"
很明显,加上where 1 = 1后,代码的拓展性大大增加而且更加简洁。在后续为SQL加入限制条件时会非常的方便。
此外,SQL查询引擎在执行SQL时,如果遇到where 1 = 1 它会忽略这个条件,因此where 1 = 1 对SQL的性能并没有影响。
关于SQL中的 where 1 = 1 的用法的更多相关文章
- sql中的group by 和 having 用法解析
转载博客:http://www.cnblogs.com/wang-123/archive/2012/01/05/2312676.html --sql中的group by 用法解析:-- Group B ...
- sql中exists和not exists的用法
该文转载自:http://www.cnblogs.com/mytechblog/articles/2105785.html sql中exists,not exists的用法 exists : 强调的是 ...
- sql中的group by 和 having 用法
sql中的group by 用法:-- Group By语句从英文的字面意义上理解就是“根据(by)一定的规则进行分组(Group)”.--它的作用是通过一定的规则将一个数据集划分成若干个小的区域,然 ...
- SQL中笛卡尔积-cross join的用法
在数学中,笛卡尔乘积是指两个集合X和Y的笛卡尓积(Cartesian product),又称直积,表示为X × Y,第一个对象是X的成员而第二个对象是Y的所有可能有序对的其中一个成员 假设集合A={a ...
- SQL中Group By和having的用法
转自 ITGirl笑笑 一.GROUP BY GROUP BY语句用来与聚合函数(aggregate functions such as COUNT, SUM, AVG, MIN, or MAX. ...
- sql中union和union all的用法
如果我们需要将两个select语句的结果作为一个整体显示出来,我们就需要用到union或者union all关键字.union(或称为联合)的作用是将多个结果合并在一起显示出来. union和unio ...
- SQL中的delete和TRUNCATE的用法
TRUNCATE TABLE 表名 删除表中的所有行,而不记录单个行删除操作. 语法 TRUNCATE TABLE name 参数 name 是要截断的表的名称或要删除其全部行的表的名称. 注释 TR ...
- sql中的 IF 条件语句的用法
IF 表达式 IF( expr1 , expr2 , expr3 ) expr1 的值为 TRUE,则返回值为 expr2 expr1 的值为FALSE,则返回值为 expr3 如下: SELECT ...
- SQL中的ALL,ANY,SOME的用法
准备两个表: --T1(2,3)--T2(1,2,3,4) --ALL,ANY,SOME 的子查询-- >ALL 父查询中的结果集大于子查询中每一个结果集中的值,则为真SELECT * FROM ...
随机推荐
- String数组转int数组
假设我们有一个字符串数组: String[] strings = {"1", "2", "3"}; 使用Lambda表达式(自Java 8起 ...
- 牛客NOIP暑期七天营-提高组1
牛客NOIP暑期七天营-提高组1 链接 A 边权可为0就排序建一条链子. 但是边权不为0 除了第一个有0的不行. x连向上一个比他小的数. 期间判断有无解. #include <bits/std ...
- web服务本质
目录 django 框架引入: web框架本质 HTTP协议 多功能web服务 封装,分发处理 django 框架引入: web框架本质 web框架本质: 软件开发架构: c / s ; b/ s - ...
- struct utmp
utmp结构体定义如下: structutmp { short int ut_type; // 登录类型 pid_t ut_pid; // login进程的pid char ut_line[UT_LI ...
- 团体项目(饱了嘛)_第一组_UML图
UML图 需求分析报告 https://www.cnblogs.com/Clover-yee/p/11771395.html 类图 user(用户类):主要保存用户的基本信息 shop(商铺类):主要 ...
- Ubuntu16.04安装Filebeat
Filebeat官方文档地址 https://www.elastic.co/guide/en/beats/filebeat/current/filebeat-installation.html 下载和 ...
- 【Activiti学习之四】Activiti API(三)
环境 JDK 1.8 MySQL 5.6 Tomcat 7 Eclipse-Luna activiti 6.0 一.启动流程 多种方式启动 package com.wjy.pro; import or ...
- Oracle 'no privileges on tablespace 表空间名称' 问题解决
create user bryan identified by bryan; grant create session to bryan; grant create table to brya ...
- unity延迟加载图片
把加载图片所需要的信息封装成一个任务(自己写的类,包括路径,回调等信息),再将该任务添加到自己写的任务池中(在update中执行任务委托),由于只是添加任务操作,加载完成后自动调用回调函数实例化,对主 ...
- Effective.Java第23-33条(泛型相关)
23. 类结构层次优于标签类 有时你会碰到一个类,它的实例有一个或多个风格,并且包含一个tag属性表示实例的风格.例如,如下面的类表示一个圆或者矩形: public class Figure { / ...