首先,明白一个前提条件:
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语句的一种方法。
有帮助就点个赞吧!



