Where 1 = 1; this condition is always true. In the case of an indefinite number of query conditions,1=1You can easily standardize statements.
I. Do not use where1 = 1Troubles in Multi-condition Query
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, it is dynamically constructed based on the normal query statement,CodeIt 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 = 'wavelet village, 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.
Ii. Use where1 = 1Benefits
If we change the preceding statement:
String mysqlstr = "select * from table where1 = 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 where1 = 1 and age = '18'And address = 'xiaobo youcun, 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 memory because it does not need to save the result set.
Create Table newtable as select * From oldtable where1=0Create a new table. The structure of the new table is the same as that of the queried table.