Tell you where 1 = 1

Source: Internet
Author: User

What kind of query statement is this? First of all, 1 = 1 is not any keyword in the query statement. Therefore, you can rest assured that no matter whether you use this statement or not, there is no loss for you.

In addition, many websites have introduced the select * from table where 1 = 1 statements. In addition, these statements make people more confused, I don't know what I'm talking about. As a result, many new users are reluctant to take the necessary measures, so they can feel at ease.

This article is designed for you to explain this statement. After reading this article, you will be able to open the cloud and drive your way.

Let's take a look at the result of this statement: select * from table where 1 = 1, where 1 = 1. Since 1 = 1 is always TRUE, TRUE is returned, and the condition is TRUE. Therefore, this statement is equivalent to select * from table and returns all data in the query table.

I. You do not need to worry about the problem where 1 = 1 in Multi-condition queries.

For example, if you query a page and have multiple options to query, you can also select and enter the query keyword on your own, 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'";
}

① Assumptions

If both of the above two if judgment statements are true, that is, if you have entered query words, the final mysqlstr Dynamic Construction statement becomes:

Mysqlstr = "select * from table where age = '18' and address = 'xiaobo youcun, guangnan County, Wenshan Prefecture, Yunnan Province '"

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

② Assumptions

If the above two if judgment statements are not true, the final mysqlstr Dynamic Construction statement becomes:

Mysqlstr = "select * from table where"

Now, let's take a look at this statement. Because the where keyword must be followed by conditions, but this statement does not have any conditions, this statement is an incorrect statement and cannot be executed, not only is an error reported, but no data is found.

The preceding two assumptions represent the actual application. They indicate that the statement structure is not sufficient to cope with flexible and variable query conditions.

2. advantages of using where 1 = 1

If we change the preceding 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:

① Assumptions

If both of the IF statements are true, the statement becomes:

Mysqlstr = "select * from table where 1 = 1 and age = '18' and address = 'wavelet village, guangnan County, Wenshan Prefecture, Yunnan Province '", obviously, this statement is a correct statement that can be correctly executed. If the database has records, it will certainly be queried.

② Assumptions

If neither of the two if statements is true, the statement becomes:

Mysqlstr = "select * from table where 1 = 1". Now, let's look at this statement. Because where 1 = 1 is a true statement, the syntax of this statement is correct, it can be correctly executed, and its function is equivalent to: mysqlstr = "select * from table", that is, returning all data in the table.

The implication is: if you do not select any fields or enter any keywords on the Multi-condition query page, all data in the table will be returned. If you are on the page, if some fields are selected and some query keywords are entered, the query is performed based on the conditions set by the user.

Speaking of this, I don't know whether you understand it. In fact, the application where 1 = 1 is neither an advanced application nor an intelligent construction, it is just a method used to construct a dynamic SQL statement that can run correctly to meet the uncertainties in the Multi-condition query page.

3. Do I have to use where 1 = 1 to perform multiple conditional queries?

No, you can use it whenever you like. If you don't like it, you won't use it. For you, there will be no loss or more;

So, if I do not use where 1 = 1 for multi-condition queries, how can I construct a dynamic query statement? It is very simple and provides you with the following thinking:

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

No matter whether you use a multi-condition query that does not use where 1 = 1, as long as you can ensure that the query statements you have constructed are correct, it is foolproof.

Iv. Summary of where 1 = 1

It is a "" method to facilitate the construction of complex and correct query statements with dynamic multi-condition uncertainties ".

This method is generally not common in books, but in practical applications, people have to consider it from a realistic perspective, that is to say, it is necessary to ensure that the multi-condition query can be met, and the flexibility to cope with uncertainties. Finally, it is necessary to ensure that the statement does not have any syntax errors.

This method is a good method. However, since where 1 = 1 does not know how many new users are allowed, the number of times it has been pondered is never necessary. At the same time, it may mislead new users into the wrong path;

I hope this article will help you.

From: http://www.dzwebs.net/2418.html

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.