[go to Dachang series] deeply understand the use of where 1 = 1 in MySQL

First , Understand a prerequisite :

where 1 = 1; -- Always true 

in other words :

select * from student;
Equate to :
select * from student where 1 = 1;

look ,where 1 = 1 Is a condition that is always true , It's no use . But when we add dynamics SQL After the statement , The result is different :


No, where 1 = 1 when

select * from student
where
and condition1
and condition2
and ...

When all condition All for false when , The statement becomes :

select * from student where;

This SQL The statement is grammatically wrong .


Yes where 1 = 1 when

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

When all condition All for false when , The statement becomes :

select * from student where 1 = 1;

because where The condition after the statement is true, Therefore, this statement is logically equivalent to

select * from student;

summary

where 1 = 1 Statement for dynamic SQL In the sentence , It is a method to construct a correct and running dynamic query in order to meet the uncertain factors in the multi condition query page SQL A method of statement .


If it's helpful, just like it !

Please bring the original link to reprint ,thank
Similar articles

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14

2021-10-14