where 1=1 and 0=1 in SQL statements

Source: Internet
Author: User

Excerpt from: http://blog.sina.com.cn/s/blog_afe616ab0101camd.html

The role of SQL where 1=1 and 0=1

where 1=1; This condition is always true, and in the case of an indefinite number of query conditions, the 1=1 can be a convenient specification statement.

For example, if you make a query page, let the user choose and enter the query keyword, 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 '";

}

If the above two if judgment statements do not hold, then the final mysqlstr dynamic construct statement becomes: mysqlstr= "select * from table where" the statement is an incorrect statement.

where 1=0; This condition is always false, and the result does not return any data, only the table structure, which can be used to quickly build the table "select * from strName WHERE 1 = 0";   This SELECT statement is primarily used to read the structure of a table without regard to the data in the table, saving memory because you can save the result set.  CREATE TABLE newtable as SELECT * from oldtable where 1=0; Creates a new table, and the structure of the new table is the same as the structure of the queried table.

where 1=1 and 0=1 in SQL statements

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.