[sql] The role and benefits of using where 1 = 1 and 0 = 1 in mysql

The use and benefits of where 1 = 1 and 0 = 1 in mysql

Primer

When using php to operate mysql, often use where statement to query. When the where statement does not exist, often add a where 1 = 1 at the back, this is based on my colleague's writing. Let's analyze the benefits of where 1 = 1!

where 1 = 1; This condition is always True. In the case of an indefinite number of query conditions, 1 = 1 can easily standardize the statement.

1. Don't worry about where 1 = 1 in multi-condition query

  For example, if you make a query page, and there are multiple options that can be queried, and at the same time, let the user choose and enter query keywords, then, according to the dynamic structure of the usual query statement, the code is roughly as follows:

string MySqlStr=”select * from table where”;

  if(Age.Text.Lenght>0)
  {
    MySqlStr=MySqlStr+“Age=“+“'Age.Text'“;
  }

  if(Address.Text.Lenght>0)
  {
    MySqlStr=MySqlStr+“and Address=“+“'Address.Text'“;
}

The first hypothesis
If the above two IF judgment statements are both True, that is, the user has entered a query term, then the final MySqlStr dynamic construction statement becomes

MySqlStr=”select * from table where Age='18'  and Address='云南省文山州广南县小波吗村'”

It can be seen that this is a complete and correct SQL query statement that can be executed correctly and returns data based on whether there are records in the database.

The second assumption
If the above two IF judgment statements are not true, then the final MySqlStr dynamic construction statement becomes:

MySqlStr=”select * from table where“

Now, let's take a look at this statement. Since the condition needs to be used after the where keyword, but there is no condition at all in this statement, so this statement is an erroneous statement and it must not be executed. Will query any data.

The above two assumptions represent a practical application, indicating that there are problems with the construction of the sentence, which is not enough to cope with the flexible query conditions.

Second, the benefits of using where 1 = 1

If we change the above statement to:

string MySqlStr=”select * from table where  1=1 ”;


  if(Age.Text.Lenght>0)
  {
    MySqlStr=MySqlStr+“and Age=“+“'Age.Text'“;
  }

  if(Address.Text.Lenght>0)
  {
    MySqlStr=MySqlStr+“and Address=“+“'Address.Text'“;
}

Now, there are also two assumptions

The first assumption

If both IFs hold, then the statement becomes:

MySqlStr=”select * from table where  1=1 and Age='18'  and Address='云南省文山州广南县小波吗村'”

Obviously, this statement is a correct statement and can be executed correctly. If there is a record in the database, it will definitely be queried.

The second hypothesis

If neither IF holds, then the statement becomes:

MySqlStr=”select * from table where 1=1”,

Now, let's take a look at this statement. Since where 1 = 1 is True, the statement has correct syntax and can be executed correctly. Its function is equivalent to: MySqlStr = ”select * from table”, that is, return All data in the table.

The implication is: if the user does not select any fields or enter any keywords on the multi-condition query page, then all data in the table will be returned; if the user is on the page, select some fields and enter some Query keywords, then, query according to the conditions set by the user.

Speaking of which, I do n’t know if you have understood. In fact, the application of where 1 = 1 is not an advanced application or a so-called intelligent structure, just to meet various uncertain factors in the multi-condition query page A method used to construct a dynamic SQL statement that can run correctly.

where 1 = 0; this condition is always false, the result will not return any data, only the table structure, can be used to quickly build the table

"SELECT * FROM strName WHERE 1 = 0"; 

The select statement is mainly used to read the structure of the table without considering the data in the table, which saves memory, because you can save the result set.

create table newtable as select * from oldtable where 1=0;  

Create a new table, and the structure of the new table is the same as the structure of the query table.

Guess you like

Origin www.cnblogs.com/iiiiiher/p/12718792.html