Why would someone use WHERE 1=1 AND in a SQL clause?

后端 未结 19 1973
甜味超标
甜味超标 2020-11-22 07:08

Why would someone use WHERE 1=1 AND in a SQL clause (Either SQL obtained through concatenated strings, either view definition)

I\'ve

19条回答
  •  囚心锁ツ
    2020-11-22 07:27

    While I can see that 1=1 would be useful for generated SQL, a technique I use in PHP is to create an array of clauses and then do

    implode (" AND ", $clauses);
    

    thus avoiding the problem of having a leading or trailing AND. Obviously this is only useful if you know that you are going to have at least one clause!

提交回复
热议问题