In simply sql book, there is portion of code mentioning if I use where 1 = 0, i can safely add an OR conditions like:
WHERE
1=0
OR name LIKE '%Toledo%'
OR billaddr LIKE '%Toledo%'
OR shipaddr LIKE '%Toledo%'
I didnt get it why when i use where 1 = 0 i can safely add an OR statement
Can i just use something like below?
WHERE
1=1
OR name LIKE '%Toledo%'
OR billaddr LIKE '%Toledo%'
OR shipaddr LIKE '%Toledo%'
I understand that where 1=1 is a shortcut to add an And statement.
I understand that because where 1 =1 will return true.Can i use it for Or statement too?
Istill didnt get the part where 1 = 0
Any explaination would be great.
This depends on the type of boolean operations you’re working on. If you want to add a variable number of
ANDstatements, then you use a statement that invariably evaluates to true, such as1 = 1. On the other hand, if you want to do the same withORstatements, then you should use a statement that evaluates to false, such as1 = 0.Let’s say you have a boolean variable
xwith an indeterminate truth value (it might be true, or it might be false. You don’t know.) Now, if you find the value ofx AND false, you getfalse, regardless of what the value ofxis.On the other hand, if you look at
x OR true, you’ll gettrue. Again, this is regardless of the truth value ofx.In your statement, you want the hard-coded value to have no effect on the logic of the query. Since
false OR a OR b OR cis logically equivalent toa OR b OR c, the hard-coded statement has no effect. In the other case,true AND a AND b AND cis equivalent toa AND b AND c.