SQL 语句中 where 条件后 写上1=1 是什么意思

Aidan075
• 阅读 1406

↑ 点击上方 “凹凸数据” 关注 + 星标 ~

每天更新,干货&福利不断 SQL 语句中 where 条件后 写上1=1 是什么意思

SQL 语句中 where 条件后 写上1=1 是什么意思

这段代码应该是由程序(例如Java)中生成的,where条件中 1=1 之后的条件是通过 if 块动态变化的。例如:

String sql="select * from table_name where 1=1";  
if( conditon 1) {  
  sql=sql+"  and  var2=value2";  
}  
if(conditon 2) {  
  sql=sql+"  and var3=value3";  
}

where 1=1 是为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误。

动态SQL中连接AND条件

where 1=1 是为了避免where 关键字后面的第一个词直接就是 “and”而导致语法错误。

where后面总要有语句,加上了1=1后就可以保证语法不会出错!

select * from table where 1=1

因为table中根本就没有名称为1的字段,所以该SQL等效于select * from table,

这个SQL语句很明显是全表扫描,需要大量的IO操作,数据量越大越慢,

建议查询时增加必输项,即where 1=1后面追加一些常用的必选条件,并且将这些必选条件建立适当的索引,效率会大大提高

拷贝表

create table  table_name  
as     
select * from  Source_table  
where   1=1;

复制表结构

create table  table_name  
as     
select  * from     
Source_table where   1 <> 1;

本文转转自微信公众号凹凸数据原创https://mp.weixin.qq.com/s/dVZQYmhdE7d16lVJSMb2RQ,可扫描二维码进行关注: SQL 语句中 where 条件后 写上1=1 是什么意思 如有侵权,请联系删除。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Easter79 Easter79
2年前
sql注入
反引号是个比较特别的字符,下面记录下怎么利用0x00SQL注入反引号可利用在分隔符及注释作用,不过使用范围只于表名、数据库名、字段名、起别名这些场景,下面具体说下1)表名payload:select\from\users\whereuser\_id1limit0,1;!(https://o
Wesley13 Wesley13
2年前
4cast
4castpackageloadcsv.KumarAwanish发布:2020122117:43:04.501348作者:KumarAwanish作者邮箱:awanish00@gmail.com首页:
Wesley13 Wesley13
2年前
oracle树状查询
connectby是结构化查询中用到的,其基本语法是:select...fromtablenamestartwith条件1connectby条件2where条件3;例:select\fromtablestartwithorg\_id'HBHqfWGWPy'conne
Wesley13 Wesley13
2年前
oracle游标的例子
declare    cursor ca is select id_no, name from user where ym201401;begin    for cb in ca loop        update path set enamecb.name where id_nocb.id
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Stella981 Stella981
2年前
200的大额人民币即将面世?央行:Yes!
点击上方蓝字关注我们!(https://oscimg.oschina.net/oscnet/2a1c2ac00bf54458a78c48a6c2e547d5.png)点击上方“印象python”,选择“星标”公众号重磅干货,第一时间送达!!(
可莉 可莉
2年前
200的大额人民币即将面世?央行:Yes!
点击上方蓝字关注我们!(https://oscimg.oschina.net/oscnet/2a1c2ac00bf54458a78c48a6c2e547d5.png)点击上方“印象python”,选择“星标”公众号重磅干货,第一时间送达!!(