MySQL NOT operator
We have already discussed that there are AND, OR and NOT are the three logical operators in MySQL. In the earlier posts we learnt how AND and OR operators. In this post we will see how the NOT operator works in MySQL.
NOT is one of the logical operators which reverses or negates the given input.
The NOT operator is denoted with “!” symbol.
Let us see the NOT operator under different cases with examples.
a.When an input operand is 0 then NOT operator negates ‘0’ and returns ‘1’.
b. When an input operand is 1(or any other value) then NOT operator negates ‘1’ and returns ‘0’.
c.When NULL is given as an input then NOT operator negates it and returns NULL as output.
d. When the two or more operands are provided as an input then NOT operator negates those and returns 0 as output.
Note: In the above example, for the statement 1(1+2-3) it results 0 as output. Hence NOT operator negates that “0” and returns “1” as output.
Till now we discussed about NOT operator on some operands under simple statements. Now we will see how it works on databases.
The NOT operator is often used in the WHERE clause of the SELECT, UPDATE, DELETE statement to form Boolean expressions.
The syntax of NOT operator is as follows:
|SELECT column1, column2, … FROM table_name WHERE NOT condition;|
For demonstration I am choosing my sample database and is shown in below:
In the above statement, we gave the condition as Country=”Spain”. This NOT operator negates the statement and returns the output of the columns excluding the columns where the country name as Spain.
Combining AND,OR and NOT
We can also AND, OR and OR operators.