Where 1 = 1 or 0 = 0

Source: Internet
Author: User

SQL where 1 = 1 and 0 = 1
Where 1 = 1; this condition is always True. In the case of an indefinite number of query conditions, 1 = 1 can be a convenient standard statement.
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.
Where 1 = 0; this condition is always false, and no data is returned. Only the table structure is available for quick table creation.
"SELECT * FROM strName WHERE 1 = 0"; this select statement is mainly used to read the table structure without considering the data in the table. This saves the memory, because you do not need to save the result set.
Create table newtable as select * from oldtable where 1 = 0; create a new table with the same structure as the queried table.

Author: "disconnected and disconnected"
 

Related Article

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.