In the earlier post we have discussed that there are three logical operators AND, OR and NOT. We already gone through AND operator in the last post. Now, In this post we will see how OR operator works in MySQL.
The OR operator combines two Boolean expressions and returns to TRUE if any one of the two statements evaluates to TRUE. It is represented with “| |” symbol.
But what if there is more than one logical operator (i.e., combination of AND and OR) exists in a statement…At that time OR operator perform after the evaluation of AND operator. We use parentheses to change the evaluation order.
Let us discuss about OR operator on simple statements.
a. OR operator returns 1 when one operand is non-NULL and the other operand is Non zero.
b. OR operator returns 1 when an operand is Non-NULL and the other operand is Zero.
c. OR operator returns zero when both the operands are zero.
d.OR operator returns “NULL” when one operand NULL and the other operand is zero.
e. OR operator returns “1” when one operand is NULL and the other operand is non-zero.
f. OR operator returns “NULL” when both the operands are “NULL”.
In MySQL, Notice that 0 is FALSE and other than 0 are TRUE.
Till now we have seen how OR operator in MySQL works on simple statements. Let us discuss how OR operator in MySQL works with databases…
The OR operator is often used in the WHERE clause of the SELECT, UPDATE, DELETE statement to form Boolean expressions.
The syntax of OR operator is as follows:
|SELECT column1, column2, … FROM table_name WHERE condition1 OR condition2 OR condition3 …;|
I am taking my sample database for the demonstration on OR operator.
In the above example we are combining only two expressions. But we can also combine more than two expressions. For example,