PHP reports – Functions and Logic – POST Logic – All

1.    WHERE 1 =1

If the list of conditions is not known at compile time and is instead built at run time:

  • You can generate them all like: and <condition> and concatenate them all together.
  • With the 1=1 at the start, the initial and has something to associate with.
  • You always know that the WHERE clause is already defined and it allows you to keep adding conditions without having to check if it is the first one.
  • It saves having to do a test for conditions and then add a “WHERE” string before them.
  • While 1=1 would be useful for generated SQL, a PHP technique is to create an array of clauses and then do implode (” AND “, $clauses);
  • This avoids the problem of having a leading or trailing AND.

2.    Implode function

  • While 1=1 would be useful for generated SQL, a PHP technique is to create an array of clauses and then do implode (” AND “, $clauses);
  • This avoids the problem of having a leading or trailing AND.

NOTE: This is only useful if you know that you are going to have at least one clause!

3.     WHERE 1=2

create table t1 as select * from t   // t1 will be same as t without data.