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