`

where 1=1 and where 1=0

 
阅读更多
where 1=1有什么用?在SQL语言中,写这么一句话就跟没写一样。

select * from table1 where 1=1与select * from table1完全没有区别,甚至还有其他许多写法,1<>2,'a'='a','a'<>'b',其目的就只有一个,where的条件为永真,得到的结果就是未加约束条件的。

在SQL注入时会用到这个,例如select * from table1 where name='lala'给强行加上select * from table1 where name='lala' or 1=1这就又变成了无约束的查询了。

最近发现的妙用在于,在不定数量查询条件情况下,1=1可以很方便的规范语句。例如一个查询可能有name,age,height,weight约束,也可能没有,那该如何处理呢?

String sql=select * from table1 where 1=1

为什么要写多余的1=1?马上就知道了。

if(!name.equals("")){
sql=sql+"name='"+name+"'";
}
if(!age.equals("")){
sql=sql+"age'"+age+"'";
}
if(!height.equals("")){
sql=sql+"height='"+height+"'";
}
if(!weight.equals("")){
sql=sql+"weight='"+weight+"'";
}

如果不写1=1呢,那么在每一个不为空的查询条件面前,都必须判断有没有where字句,否则要在第一个出现的地方加where

今天看到:"SELECT * FROM strName WHERE 1 = 0";
不理解为什么有1=0?

查询得出答案:

该select语句主要用于读取表的结构而不考虑表中的数据,这样节省了内存,因为可以不用保存结果集。

另外,这个用在什么地方呢?主要用于创建一个新表,而新表的结构与查询的表的结构是一样的。如下SQL语句:

create table newtable as select * from oldtable where 1=0;

 

分享到:
评论

相关推荐

    sql where 1=1的优缺点分析

    一、不用where 1=1 在多条件查询中的困扰 举个例子,如果您做查询页面,并且,可查询的选项有多个,同时,还让用户自行选择并输入查询关键词,那么,按平时的查询语句的动态构造,代码大体如下: 代码如下: ...

    帐目查询(VB6.0源代码编写)'按日期查询并汇总商品销售信息 Adodc1.RecordSource = "select * from xsd where 日期 between " + Chr(35) + Str(DTPicker1.Value) + Chr(35) + "and " + Chr(35) + Str(DTPicker2.Value) + Chr(35) + "order by 日期"

    帐目查询(VB6.0源代码编写)'按日期查询并汇总商品销售信息 Adodc1.RecordSource = "select * from xsd where 日期 between " + Chr(35) + Str(DTPicker1.Value) + Chr(35) + "and " + Chr(35) + Str(DTPicker2.Value...

    语句生成吧

    update tbl_org_user set role_no = '000020' where diaplayname = '{0}' and role_no = '000'; 在values.txt写个如下内容 王军 董建美 董建美 李勇 刘振永 丁海涛 张琪琨 王婷婷 将会在result.txt中生成 update ...

    海信商海导航SQL修改语句

    where canstore='1' and viptype='2' update vip set CanStore='0' where canstore='1' and viptype='2' /*将非储值卡会员变成储值卡会员*/ update vip set CanStore='1' where canstore='0' and viptype='2' .....

    用友U8新引入帐套后固定资产模块出错的解决方法

    select * from accinformation where csysid='FA' and cname='bFirstTime' --2.设置固定资产为首次启用 update accinformation set cvalue='TRUE' where csysid='FA' and cname='bFirstTime' --3.查询最新会计期间 ...

    sys_area.sql

    select id,name,lat,lng,pinyin from sys_area where 1=1 and `level` = 1; city 市 -- 江西 select id,name,lat,lng,pinyin from sys_area where 1=1 and `level` = 2 and find_in_set('360000',`path`); ...

    oracle数据库实验代码

    (1)求供应工程J1零件的供应商号码SNO: select sno from spj where jno = 'J1'; (2)求供应工程J1零件P1的供应商号码SNO: select sno from spj where jno ='J1' and pno = 'P1'; (3)求供应工程J1零件为红色的供应商...

    网狐实物点卡生成(带数据库脚本和存储过程)

    IF EXISTS (SELECT TreasureCode FROM Treasure WHERE TreasureCode=@cardcode And TreasurePass=@cardpass) BEGIN IF (Select IsPost From Treasure WHERE TreasureCode=@cardcode)=1 Begin Select ...

    pb学生成绩管理系统

    if exists(select 1 from sys.sysforeignkey where role='FK_CHOOSE_C_CHOOSE_CO_COURSES') then alter table choose_course delete foreign key FK_CHOOSE_C_CHOOSE_CO_COURSES end if; if exists(select 1 from...

    [详细完整版]9数据库.doc

    select sname, sage from student where sdept = '计算机系' and ssex='男' and sage between 18 and 20 select sname, sage from student where sdept = '计算机系' and sage&gt;=18 and sage &lt;=

    Deep Thinking Where Machine Intelligence Ends and Human Creativity Begins azw3

    Deep Thinking Where Machine Intelligence Ends and Human Creativity Begins 英文azw3 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    c#操作数据库,史上最牛逼的方法,你见过这种方法吗?

    .Where(a.UserId == 1 && (a.UserName + "123").LikeLeft(u1.UserName)) ); //下面是子查询方式的多表查询In 的使用方法 var ListUser8= Sql.ExecuteList((a, b, c) =&gt; Sub.Top(12).Select().From(a).Where(a....

    [详细完整版]4数据库.docx

    1 77 例4-9 检索供应零件给编号J1的工程的供应商编号SNO SETLEC DISTINCT SNO FROM SPJ WHERE JNO='J1' 2 77 例4-9 检索供应零件给工程J1,且零件编号为P1的供应商编号SNO SELECT SNO FROM SPJ WHERE JNO='J1' AND ...

    图书管理系统--SQL server 2000后台数据库处理

    if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[readertype]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [dbo].[readertype] GO if exists (select * from dbo....

    简单的sql注入问题

    sql:select* from user where username=‘123’ and password=‘a’ or ‘a’=‘a’ ** // 1.获取链接 conn=JDBCUtils.getConnection(); //2.定义语句的SQL sql=select* from user where username='+...

    sql添加表列字段描述添加默认值删除字段

    if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) create table [Table1] ([col1] int,[col2] int) //添加字段 if not exists...

    sql手注笔记.docx

    id=1' and 1=(select 1 from information_schema.tables where table_schema='security' and table_name regexp '^[a-z]' limit 0,1)--+ 联合注入 id=-1 ' union select 1,schema_name,3 from informaiton_...

    数据库实验报告:实验五.doc

    数学系所有学生成绩改成0 update student set Sage=0 where Sdept='ma'; 把低于总平均成绩的女同学成绩提高5分 update sc set Grade=Grade+5 where grade&lt;(select avg(grade) from sc)and sno in(select sno from ...

    Web应用安全:使用SQL注入绕过认证.pptx

    使用SQL注入绕过认证 使用SQL注入绕过认证 1、SQL注入分类 SQL注入类型 ...SELECT * FROM table WHERE id=1 and 1 = 1 。若语句正确,执行正常,则返回的数据与原始请求无任何差异。 ③ 在输入框中

    sql备份-2019年最新三级城市数据(国家行政地区编码)--

    UPDATE info3 f,info3 u SET u.p_id=f.id WHERE u.shengJi=f.shengJi AND u.`xianJi` = 'NULL' AND f.diJi='0' AND f.xianJi='0' -- 把所有省级P_ID设置为0 UPDATE info3 SET p_id=0 WHERE diJi='0' AND xianJi='0' ...

Global site tag (gtag.js) - Google Analytics