1
Logical and Comparison Operators
2
================================
4
============================== ================================
6
============================== ================================
7
:ref:`and` && Logical AND
8
:ref:`or` (||) Logical OR
10
:ref:`not` (!) Logical NOT
11
:ref:`less_than` < less than
12
:ref:`greater_than` > greater than
13
:ref:`less_or_equal` <= less than or equal to
14
:ref:`greater_or_equal` >= greater than or equal to
16
:ref:`not_equal` <> or != not equal
17
============================== ================================
25
This logical operator AND:
27
* evaluates to 1 if all operands are nonzero and not NULL
28
* evaluates to 0 if one or more operands are 0
29
* otherwise returns NULL
44
AND can be used to select rows that satisfy all the conditions given in a statement. For Example, to find the names of the students between the age 20 to 25 years, the query would be like:
48
SELECT first_name, last_name, age
50
WHERE age >= 20 AND age <= 25;
54
+---------------+------------------+-------+
55
|first_name |last_name |age |
56
+===============+==================+=======+
58
+---------------+------------------+-------+
60
+---------------+------------------+-------+
61
|Sheila |Donaldson |25 |
62
+---------------+------------------+-------+
64
The logical "AND" operator selects rows only if the data in all relevant columns is satisfied. In this case, 'first_name' and 'last_name' simply need to have non-NULL values, and 'age' needs to be a value between 20 and 25.
71
This logical operator OR:
73
* returns 1 if any operand is nonzero and both operands are non-NULL, and returns 0 otherwise
74
* returns 1 with a NULL operand if the other operand is nonzero, and retunes NULL otherwise
75
* returns NULL if both operands are NULL
90
In other words, OR is used to select rows that satisfy at least one of the given conditions.
92
For example, the following query could be used to find the user_id for people that live in either California or Texas:
98
WHERE state = 'California' OR state = 'Texas';
100
The result set could be something like:
102
+---------------+------------------+-----------+
103
|user_id |city |state |
104
+===============+==================+===========+
105
|608 |Sacremento |California |
106
+---------------+------------------+-----------+
107
|844 |Austin |Texas |
108
+---------------+------------------+-----------+
109
|917 |Oakland |California |
110
+---------------+------------------+-----------+
118
* returns NULL if either operand is NULL
119
* evaluates to 1 for non-NULL operands (if an odd number of operands is nonzero)
120
* otherwise 0 is returned
122
.. code-block:: mysql
128
SELECT 1 XOR 1 XOR 1;
133
Note that "a XOR b" is the mathematical equivalent of (a AND (NOT b)) OR ((NOT a) and b).
141
This logical operator NOT:
143
* evaluates to 1 if the operand is 0
144
* evaluates to 0 if the operand is nonzero
145
* NOT NULL returns NULL
147
.. code-block:: mysql
160
If you want to find rows that do not satisfy a condition, you can use the logical operator, NOT. NOT results in the reverse of a condition. That is, if a condition is satisfied, then the row is not returned.
162
For example: If you want to find out the user_id for people who do not practice medicine as their profession, the query would be like:
164
.. code-block:: mysql
166
SELECT user_id, title, occupation
167
FROM user_occupations
168
WHERE NOT occupation = 'Doctor';
170
The result set would be something like:
172
+---------------+------------------+--------------+
173
|user_id |degree |occupation |
174
+===============+==================+==============+
175
|322 |PhD |Professor |
176
+---------------+------------------+--------------+
178
+---------------+------------------+--------------+
179
|681 |MD |Consultant |
180
+---------------+------------------+--------------+
190
.. code-block:: mysql
203
.. code-block:: mysql
215
.. code-block:: mysql
221
.. _greater_or_equal:
223
GREATER THAN OR EQUAL
224
----------------------
226
Greater than or equal:
228
.. code-block:: mysql
240
.. code-block:: mysql
261
.. code-block:: mysql
263
SELECT '.01' <> '0.01';
265
SELECT .01 <> '0.01';
267
SELECT 'zing' <> 'zingg';