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

后端 未结 19 2214
甜味超标
甜味超标 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:48

    I've seen it used when the number of conditions can be variable.

    You can concatenate conditions using an " AND " string. Then, instead of counting the number of conditions you're passing in, you place a "WHERE 1=1" at the end of your stock SQL statement and throw on the concatenated conditions.

    Basically, it saves you having to do a test for conditions and then add a "WHERE" string before them.

提交回复
热议问题