Why write select * from table where 1 = 1

 On many sites, we often see select * from table where 1 = 1 such a query;

  This is a kind of query it? First, 1 = 1 is not a keyword query any statement, so please be assured that whatever you will not use this statement, have nothing to do, for your purposes, without any loss.

  In addition, many sites have a select * from table where 1 = 1 introduction of such statements, and for statements that class, put it is people getting more and more confused (an exact copy of a simply outrageous), I do not know What to say, lead to a lot of newcomers to no avail, so their heart.

  This article is designed for you to explain the statement, reading this article, you will cut through the fog enlightened.

  Let's take a look at the results of this statement: select * from table where 1 = 1, where where 1 = 1, 1 = 1 is always due to the establishment, returns TRUE, the condition is true; therefore, this statement is equivalent to select * from table, the query returns all the data in the table.

  First, do not trouble where 1 = 1 in the multi-criteria query

  For example, if you make a query page, and the option to have multiple queries, while also allowing users to choose and enter the query keywords, then, according to the usual dynamic query construction 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'“;
  }

  ① hypotheses

  If the above two judges IF statements are True, that is, users enter a query term, then, the final MySqlStr dynamically constructed statement becomes:

  MySqlStr = "select * from table where Age = '18 'and Address =' ​​Wenshan Guangnan County village wavelet it. '"

  Can see, this is a complete correct SQL query can be executed correctly, and whether there are records based on database and return data.

  ② hypotheses

  If the above two statements IF judge does not hold, then the final MySqlStr dynamically constructed statement becomes:

  MySqlStr=”select * from table where“

  Now, let's look at this statement, due to the need to use keywords where the back condition, but this statement does not exist conditions, so the statement is a false statement, certainly can not be executed, not only an error, but not queries to any data.

  Both of the above assumptions, represent real-world applications, description, problematic construction statement, is not sufficient to cope with flexible search criteria.

  Second, the use of benefits where 1 = 1

  If we read the above statement:

  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 two assumptions

  ① hypotheses

  IF If both are true, then the statement becomes:

  MySqlStr = "select * from table where 1 = 1 and Age = '18 'and Address =' ​​Wenshan Guangnan County village wavelet it '," it is clear that the statement is a correct statement, is capable of performing correctly, if database record will certainly be queried.

  ② hypotheses

  If the two IF is not true, then the statement becomes:

  = MySqlStr "the SELECT * from the Table where 1 = 1" , and now, we look at this statement, where 1 = 1 is due to True statement, therefore, that section grammatically correct sentence, can be executed correctly, it's a considerable role in: MySqlStr = "select * from table ", i.e., all data in return.

  The implication is this: If a user in a multi-criteria query page, do not select any field, do not enter any keyword, then will return to the table all the data; if the user is on a page, select some fields and some input query keywords, then, under the conditions set by the user's query.

  Having said that, I do not know whether you have to understand, in fact, where 1 = 1 application, not what advanced applications, nor is the so-called intelligent construction, just to meet a variety of factors multi-criteria query page uncertain and a method of constructing dynamic SQL statements can be used in a proper run.

  Third, I have to use where 1 = 1 you can do multi-criteria query

  Definitely, you would love to use to use, you do not like to use is not used for your purposes, what is not lost, it will not get anything;

  Well, if I do not use 1 = 1 conditions where queries do more, how to construct dynamic queries it? Very simple, the next to give you a way of thinking:

  The following statement:

  string MySqlStr=”select * from table”;
  

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

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

  if(QuerySqlStr.Lenght>0)
  {
    MySqlStr=MySqlStr+“ where “+QuerySqlStr;
  }

  Whether you use do not use 1 = 1 conditions where queries do more, as long as you can guarantee that your constructed query is correct foolproof.

  Four, where 1 = 1 summary

  The correct one for the complex query conditions of uncertainty and more convenient to construct dynamic taken "means rivers and lakes."

  Such methods, in general, are not common in the books, but in practical applications, people have to be considered from a realistic point of view, that is, to ensure the maximum multi-criteria query, but also the flexibility to cope with uncertainty and, finally, ensure that any statement syntax errors do not occur.

  This method is a good way; however, since this was  where 1 = 1  do not know how to let a novice, wondering how many times, always to no avail, at the same time, there is likely to mislead the novice astray;

  I hope this helps.

 

Reprinted from http://www.dzwebs.net/2418.html

 

Guess you like

Origin www.cnblogs.com/ChangeMyWorld/p/10958429.html