Knowledge sharing: 1 = 1 after where condition in SQL statement is

Time:2021-2-24

 

This code should be generated by the program. The condition after 1 = 1 in the where condition is dynamically changed through the if block. For example:

  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″;

}

Where 1 = 1 is to avoid grammatical errors caused by the first word after the where keyword is “and”.

Join and condition in dynamic SQL

Where 1 = 1 is to avoid grammatical errors caused by the first word after the where keyword is “and”.

There must always be a statement after where. After adding 1 = 1, you can ensure that the syntax will not make mistakes!

select * from table where 1=1

Because there is no field named 1 in the table, the SQL is equivalent to select * from table,

This SQL statement is obviously a full table scan, which requires a lot of IO operations. The larger the amount of data, the slower it will be,

It is suggested to add a required item in the query, that is, some common required conditions should be added after where 1 = 1, and an appropriate index should be established for these required conditions, which will greatly improve the efficiency

Copy table

create table  table_name  as  select  *  from  Source_table  where  1=1;

Copy table structure

create table  table_name  as  select  *  from  Source_table  where  1 <> 1;

 

 

In addition, if you want to better improve your programming ability, learn C language c + + programming! Overtaking on the curve, one step faster! I may be able to help you here~

Up has uploaded some video tutorials for learning C / C + + programming on its home page. Those who are interested or learning must have a look! It will help you~

Share (source code, project video, project notes, basic introductory course)

Welcome to change and learn programming partners, use more information to learn and grow faster than their own thinking Oh!

Free learning books:

 

Free learning materials: