Why SQL statement plus 1=1

Source: Internet
Author: User

is to link the following query condition criteria, or to replace statements that do not have a query condition.
For example, to pass a retrieval condition as a parameter to SQL, then it can be assigned a value of 1=1 when the retrieval statement does not exist. This avoids SQL errors, and you can combine conditional SQL with non-conditional SQL.

This is to facilitate the concatenation of SQL conditions, in the "where" and "and" where the convenience of processing (here with "and" keyword example)


If you have the following 5 column conditions can be any combination:
A= ' a '; b= ' B '; C= ' C '; D= ' d '; E= ' E '

The first scenario:
If the initial SQL is: SELECT * from t1

When we generate the final SQL, we need to judge the situation.
1. Without a condition, SQL remains the same
2. If there is at least one condition, we need to add a "where" after SQL to join the condition
3. If there are at least two conditions, the first condition after "where" does not require "and", because the 5 column conditions can be arbitrarily combined, we need to determine which column is immediately after the "where", and then need the code to judge

The second scenario:
If the initial SQL is this: SELECT * from T1 where 1=1

Now, for any of the scenarios in the first scenario, we just need to add "and + corresponding conditions" directly behind the SQL, without having to write another code to determine
Like what:
sql = SQL + ' and ' + a= ' a '---> select * from t1 where 1=1 and a= ' a '
sql = SQL + ' and ' + b= ' B '---> select * from t1 where 1=1 and b= ' B '
sql = SQL + ' and ' + c= ' C ' + "and" + d= ' d '---> select * from t1 where 1=1 and c= ' C ' and d= ' d '

Especially when there are many conditions, it is convenient for the program to be able to cycle dynamic adding conditions.
Here's a pseudo-code example:
sql = select * from t1 where 1=1;
for (int i = 0; i < column. Count; i++)
{
sql = SQL + "and" + column[i]. Name + "= '" + column[i]. Value + "'";
}
The resulting SQL is similar to the following:
SELECT * from T1 where 1=1 and A= ' a ' and b= ' B ' and c= ' C ' and d= ' d ' and e= ' e '

Why SQL statement plus 1=1

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.