Can anyone please explain Why we are using where 1=1
in SQL select query?
Select * from <TableName>
Where 1=1
<cfif isdefined('Something)>
AND columnName = value
</cfif>
Can anyone please explain Why we are using where 1=1
in SQL select query?
Select * from <TableName>
Where 1=1
<cfif isdefined('Something)>
AND columnName = value
</cfif>
Tools often use this to let them append AND some_other_condition to the query, without having to worry whether this is the first predicate or not.
If you write
SELECT * FROM A
...and want to add a predicate you need to append "WHERE C1=1", but then adding a second predicate would append "AND C2=1"
But if you write
SELECT * FROM A WHERE 1=1
...you can just append "AND C1=1" for the first, and "AND C2=1" for the second
I have seen and used 'Where 1 = 0' to just create a new table with the same schema but without any data. But this one escapes me. The where clause is just not required here and the query would give the same result with or without the where clause.