SQL where 1 = 1 and 0 = 1

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 easily regulate the statement. 1. You do not need to worry about where 1 = 1 in Multi-condition queries. For example, if you query a page and there are multiple options that can be queried, at the same time, the user is also allowed to select and enter the query keyword on his/her 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 ';} ① suppose that IF both of the above-mentioned IF judgment statements are True, that is, the query word is entered by the user, the final MySqlStr Dynamic Construction statement becomes: mySqlStr = "select * from table where Age = '18' and Ad Dress = 'xiaobo youcun, guangnan County, Wenshan Prefecture, Yunnan Province '"can tell that this is a complete and correct SQL query statement that can be correctly executed and is recorded based on whether the database exists, return data. ② Suppose that 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, the where keyword must be followed by conditions, but this statement does not have any conditions. Therefore, this statement is an incorrect statement and cannot be executed, 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. advantages of using where 1 = 1 if we change the preceding 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 kinds of assumptions: ① IF both of them are true, then 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. ② Suppose that 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 and can be correctly executed. Its function is equivalent to: MySqlStr = "select * from table ", returns 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. It only has the table structure and can be used to quickly create a table "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 where 1 = 0; create a new table with the same structure as the queried table.

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.