栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【一起去大厂系列】深入理解MySQL中where 1 = 1的用处

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【一起去大厂系列】深入理解MySQL中where 1 = 1的用处

首先,明白一个前提条件:

where 1 = 1;   --永远为真

也就是说:

select * from student;    

等同于: 

select * from student where 1 = 1;

看起来,where 1 = 1就是一个永远为真的条件,没有什么用。 但当我们加上动态SQL语句后,结果就不同了:


没有where 1 = 1时
select * from student
where
	and condition1 
	and condition2 
	and ...

当所有的condition都为false时,该语句就变成了:

select * from student where;

这个SQL语句在语法上是错误的。


有where 1 = 1时
select * from student
where 1 = 1
	and condition1 
	and condition2 
	and ...

当所有的condition都为false时,该语句就变成了:

select * from student where 1 = 1;

由于where语句后的条件为true,因此该语句在逻辑上等同于

select * from student;

总结

where 1 = 1语句用于动态SQL语句中,是为了满足多条件查询页面中不确定的各种因素而采用的一种构造一条正确能运行的动态SQL语句的一种方法。


有帮助就点个赞吧!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/271084.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号