Why 'WHERE 1=1'?
Publish date: 2020-02-12
Why 'WHERE 1=1?
Have you ever seen code like this:
SELECTA, B, CFROM TabAWHERE 1=1AND A = 'Foo'AND B = 'Bar'
The simple answer is that, this is just for some lazy debugging.
Let's say you have the similar code:
SELECTA, B, CFROM TabAWHERE A = 'Foo'AND B = 'Bar'
and you briefly want to ignore the first filter "A = 'Foo'" for testing purposes. You would end up with
SELECTA, B, CFROM TabA--WHERE A = 'Foo'WHERE B = 'Bar'
Notice that the 'WHERE' line was commented out and therefore the next 'AND' needs to turn in a 'WHERE' to allow the syntax to remain correct.
As a result a number of developers will always make the WHERE clause 'WHERE 1=1' to avoid ever needing to comment it out and therefore never needing to change the next 'AND' to 'WHERE'.