1=1 the database from the SELECT * from table where 1=1

Source: Internet
Author: User

Many sites have a select * from table where 1=1 the introduction of such statements, and, for this class of statements, it is really to make people look more and more confused (a copy of a, simply outrageous), do not know what is said, Cause a lot of novice to make no avail, thus to its brooding.

This article, specifically for you to explain the statement, read this article, you will go through the clouds, enlightened .

Let's take a look at the result of this statement: SELECT * from table where 1=1, where the where 1=1, because 1=1 is always true, return to real, the condition is true; So, this statement is equivalent to the SELECT * from table, Returns all data in the query table.

One, without the trouble of where 1=1 in the multi-condition query

For example, if you make a query page, and, there are many options to query, but also let the user choose and input query keywords, then, according to the usual query statement dynamic construction, the code is generally 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 '";
}

Hypothesis of ①

If the above two if judgment statements are true, that is, the user has entered the query term, then the final mysqlstr dynamic construction statement becomes:

Mysqlstr= "SELECT * from table where age= ' and address= ' Yunnan province Wenshan Prefecture Guangnan County wavelet Village '"

It can be seen that this is a complete and correct SQL query statement that is executed correctly and returns data based on the existence of a record of the database.

Hypothesis of ②

If the above two if judgment statements are not true, then the final mysqlstr dynamic construct statement becomes:

Mysqlstr= "SELECT * from table where"

Now, let's take a look at this statement, because the WHERE keyword needs to use the condition, but this statement does not exist at all, so the statement is an error statement, must not be executed, not only error, but also do not query any data.

The above two hypotheses represent the application of reality, indicating that there is a problem in the construction of the statement, which is not enough to deal with the flexible query conditions.

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

Hypothesis of ①

If all two if are true, then the statement becomes:

Mysqlstr= "SELECT * from table where 1=1 and age= ' and address= ' Yunnan Wenshan Prefecture Guangnan County wavelet Village '", it is clear that the statement is a correct statement that can be executed correctly if the database has records, Will definitely be queried.

Hypothesis of ②

If none of the two if are true, then the statement becomes:

Mysqlstr= "SELECT * from table where 1=1", now let's take a look at this statement, because where 1=1 is a statement that is true, so that the syntax of the statement is correct and can be executed correctly, it functions equivalent to: mysqlstr= " SELECT * FROM table, which returns all data in the table.

The implication is: if the user in the multi-criteria query page, do not select any field, do not enter any keywords, then will return all the data in the table, if the user in the page, select a section of the field and entered a part of the query keyword, then the user set conditions to query.

Speaking of which, I do not know whether you understand, in fact, where 1=1 application, is not a high-level application, is not the so-called intelligent construction , just to meet the multi-conditional query page uncertainty of various factors and A method of constructing a dynamic SQL statement that can be run correctly .

Do I have to use where 1=1 to get a long term query?

Non-also, you love to use it, you do not like to use it, for you, do not lose anything, there will not be much;

So, how do I construct a dynamic query statement if I don't use the where 1=1 to do a long-term query? It's very simple and gives you a sense 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 a long conditional query without the where 1=1, it is foolproof if you can guarantee that the query statement you construct is correct.

Iv. Summary of where 1=1

In order to facilitate the construction of dynamic multi-condition uncertain factors, a complex and correct query statement is adopted as a "lake-state means".

Such methods, generally, are not common in books, but in practical applications, people have to consider from a realistic point of view, that is, to ensure that the multi-conditional query can be met, but also to deal with the flexibility of uncertainty, and finally to ensure that the statement does not appear any grammatical errors.

This method is a good method, but because of this where 1=1 do not know how many novice, pondering how many times, always to no avail, at the same time, there may be misleading novice astray;

1=1 the database from the SELECT * from table where 1=1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.