当前位置:Gxlcms > 数据库问题 > SQL --- where 1=1 与 1<> 1

SQL --- where 1=1 与 1<> 1

时间:2021-07-01 10:21:17 帮助过:5人阅读

select * from obgexinzi where 1=1" if (this.tbsqlUserName.Text.Trim() != string.Empty)//姓名 { strSql += " and Name like ‘%" + this.tbsqlUserName.Text.Trim() + "%‘"; } if (this.ddlUserSex.Text.Trim() != string.Empty)//性别 { strSql += " and Sex like ‘%" + this.ddlUserSex.SelectedValue + "%‘"; }

如果要是不使用 where  1=1  ,那么就要用程序进行判断什么时候使用   and

因为这样无疑是错的。

select * from obgexinzi where and Name like

而且你也很难判断,用户使用的是那几个查询条件进行组合查询,要不要加where 或直接用 and ,无疑会,提高程序复杂度

加上后,不管有没有查询条件都不会出错,只要判断条件是不是为空就行,提高运行的效率。

select * from obgexinzi where 1=1 and Name like

 

3、sql 注入,使用 or 1=1  会把表中所有数据查出来,

查寻没有什么感觉要是使用  删除语句哪

delete from  obgexinzi where Name = 周瑜 or 1=1

--删除表中所有

技术图片

 

 

4、 where  1=1  与  1<> 1  之间的关系

select * from obgexinzi where 1=1
-- 实际等效于select * from obgexinzi where true;-- 语句将返回t1中所有的记录行

select * from obgexinzi where 1<>1
-- 实际等效于 select * from obgexinzi where false;-- 语句将返回空记录集
where 1<>1 的作用

当我们只需要获取表的字段(结构)信息,而不需要理会实际保存的记录时,可以使用where 1<> 1
因为系统仅会读取结构信息,而不会将具体的表记录读入内存中,这无疑节省了系统开销。

技术图片

SQL --- where 1=1 与 1<> 1

标签:string   作用   false   text   注入   ima   use   ring   span   

人气教程排行