Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there ever a logical reason to use 1=1 in a where clause? [duplicate]

Tags:

sql

Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?

I am working on a code base that often has

where 1=1

Often, these queries are complex and difficult to read. So it's not always feasible to break down the query semantics purely for a refactor. My gut instinct is that this is always unecessary. Maybe there exists a requirement for a where clause similar to the requirement for a Group By clause when performing a SELECT with an aggregrate function alongside some column to group.

Is there ever a need for this?

like image 257
P.Brian.Mackey Avatar asked Dec 02 '11 16:12

P.Brian.Mackey


1 Answers

It makes building dynamic WHERE clauses easier...

WHERE
  1=1
  AND condition1
  AND condition2

This way you don't have to work out if you need the AND or not as you add conditions. You always need the AND.

like image 59
MatBailie Avatar answered Sep 30 '22 18:09

MatBailie