This code should be generated in a program (Java, for example) where the condition after 1 = 1 is dynamically changed by the if block. Such as:
String sql="select * from table_name where 1=1";
if( conditon 1) {
sql=sql+" and var2=value2";
}
if(conditon 2) {
sql=sql+" and var3=value3";
}
Copy the code
Where 1=1 is used to avoid syntax errors where the first word after the where keyword is “and”.
Join AND conditions in dynamic SQL
Where 1=1 is used to avoid syntax errors where the first word after the where keyword is “and”.
Where (1=1) where (1=1)
select * from table where 1=1
Copy the code
Select * from table; select * from table;
This SQL statement is obviously a full table scan, which requires a large amount of I/O operations.
You are advised to add mandatory items, that is, add common mandatory conditions after where 1=1, and create appropriate indexes for these mandatory conditions to improve query efficiency
Copy the table
create table table_name as select * from Source_table where 1=1;
Copy the code
Replicate table structure
create table table_name as select * from Source_table where 1 <> 1;
Copy the code
Source: cloud.tencent.com/developer/article/1475146