~drizzle-trunk/drizzle/development

1994.4.52 by Marisa Plumb
modifying folder structure
1
Logical and Comparison Operators
2
================================
3
1994.4.84 by Marisa Plumb
logical operators
4
==============================     ================================
5
Operators                           Description
6
==============================     ================================
7
:ref:`and` &&                       Logical AND
8
:ref:`or` (||)                      Logical OR
9
:ref:`xor` 	                    Logical XOR
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
15
:ref:`equal` =  	            equal
16
:ref:`not_equal` <> or !=           not equal
17
==============================     ================================
18
19
20
.. _and:
21
22
AND
23
---
24
25
This logical operator AND:
26
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 
30
2194.5.2 by Andrew Hutchings
Fix SQL markup
31
.. code-block:: mysql
1994.4.84 by Marisa Plumb
logical operators
32
33
	SELECT 1 && 1;
34
	        -> 1
35
	SELECT 1 && 0;
36
        	-> 0
37
	SELECT 1 && NULL;
38
        	-> NULL
39
	SELECT 0 && NULL;
40
        	-> 0
41
	SELECT NULL && 0;
42
        	-> 0
43
2194.5.2 by Andrew Hutchings
Fix SQL markup
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:
45
46
.. code-block:: mysql
1994.4.84 by Marisa Plumb
logical operators
47
48
	SELECT first_name, last_name, age
49
	FROM user_details
50
	WHERE age >= 20 AND age <= 25;
51
52
The output would be:
53
54
+---------------+------------------+-------+
55
|first_name 	|last_name 	   |age    |
56
+===============+==================+=======+
57
|Mary 	        |Bean   	   |20     |
58
+---------------+------------------+-------+
59
|Henry  	|Catson 	   |22     |
60
+---------------+------------------+-------+
61
|Sheila 	|Donaldson         |25     |
62
+---------------+------------------+-------+  
63
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.
65
66
.. _or:
67
68
OR
69
--
70
71
This logical operator OR:
72
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
76
2194.5.2 by Andrew Hutchings
Fix SQL markup
77
.. code-block:: mysql
1994.4.84 by Marisa Plumb
logical operators
78
79
	SELECT 1 || 1;
80
        	-> 1
81
	SELECT 1 || 0;
82
        	-> 1
83
	SELECT 0 || 0;
84
        	-> 0
85
	SELECT 1 || NULL;
86
        	-> 1
87
	SELECT 0 || NULL;
88
        	-> NULL
89
90
In other words, OR is used to select rows that satisfy at least one of the given conditions.
91
2194.5.2 by Andrew Hutchings
Fix SQL markup
92
For example, the following query could be used to find the user_id for people that live in either California or Texas:
93
94
.. code-block:: mysql
1994.4.84 by Marisa Plumb
logical operators
95
96
	SELECT user_id, state
97
	FROM user_location
98
	WHERE state = 'California' OR state = 'Texas';
99
100
The result set could be something like:
101
102
+---------------+------------------+-----------+
103
|user_id 	|city   	   |state      |
104
+===============+==================+===========+
105
|608            |Sacremento   	   |California |
106
+---------------+------------------+-----------+
107
|844     	|Austin 	   |Texas      |
108
+---------------+------------------+-----------+
109
|917    	|Oakland           |California |
110
+---------------+------------------+-----------+  
111
112
113
.. _xor:
114
115
XOR
116
---
117
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
121
2194.5.2 by Andrew Hutchings
Fix SQL markup
122
.. code-block:: mysql
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
123
1994.4.84 by Marisa Plumb
logical operators
124
	SELECT 1 XOR NULL;
125
        	-> NULL
126
	SELECT 1 XOR 0;
127
        	-> 1
128
	SELECT 1 XOR 1 XOR 1;
129
        	-> 1
130
	SELECT 1 XOR 1;
131
        	-> 0
132
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
133
Note that "a XOR b" is the mathematical equivalent of (a AND (NOT b)) OR ((NOT a) and b). 
1994.4.84 by Marisa Plumb
logical operators
134
135
136
.. _not:
137
138
NOT
139
---
140
141
This logical operator NOT:
142
143
* evaluates to 1 if the operand is 0
144
* evaluates to 0 if the operand is nonzero
145
* NOT NULL returns NULL
146
2194.5.2 by Andrew Hutchings
Fix SQL markup
147
.. code-block:: mysql
1994.4.84 by Marisa Plumb
logical operators
148
149
	SELECT NOT 10;
150
        	-> 0
151
	SELECT NOT 0;
152
        	-> 1
153
	SELECT NOT NULL;
154
        	-> NULL
155
	SELECT ! (1+1);
156
        	-> 0
157
	SELECT ! 1+1;
158
        	-> 1
159
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.
161
2194.5.2 by Andrew Hutchings
Fix SQL markup
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:
163
164
.. code-block:: mysql
1994.4.84 by Marisa Plumb
logical operators
165
166
	SELECT user_id, title, occupation
167
	FROM user_occupations
168
	WHERE NOT occupation = 'Doctor';
169
170
The result set would be something like:
171
172
+---------------+------------------+--------------+
173
|user_id 	|degree   	   |occupation    |
174
+===============+==================+==============+
175
|322            |PhD    	   |Professor     |
176
+---------------+------------------+--------------+
177
|579     	|PhD    	   |Writer        |
178
+---------------+------------------+--------------+
179
|681     	|MD                |Consultant    |
180
+---------------+------------------+--------------+  
181
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
182
183
.. _less_than:
184
185
LESS THAN
186
----------
187
2194.5.2 by Andrew Hutchings
Fix SQL markup
188
Less than:
189
190
.. code-block:: mysql
1994.4.95 by Marisa Plumb
more comparison operators
191
192
	SELECT 2 < 2;
193
        	-> 0
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
194
195
196
.. _greater_than:
197
198
GREATER THAN
199
-------------
200
2194.5.2 by Andrew Hutchings
Fix SQL markup
201
Greater than:
202
203
.. code-block:: mysql
1994.4.95 by Marisa Plumb
more comparison operators
204
205
	SELECT 2 > 2;
206
        	-> 0
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
207
208
.. _less_or_equal:
209
210
LESS THAN OR EQUAL
211
-------------------
212
2194.5.2 by Andrew Hutchings
Fix SQL markup
213
Less than or equal:
214
215
.. code-block:: mysql
1994.4.95 by Marisa Plumb
more comparison operators
216
217
	SELECT 0.1 <= 2;
218
        	-> 1
219
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
220
221
.. _greater_or_equal:
222
223
GREATER THAN OR EQUAL
224
----------------------
225
2194.5.2 by Andrew Hutchings
Fix SQL markup
226
Greater than or equal:
227
228
.. code-block:: mysql
1994.4.95 by Marisa Plumb
more comparison operators
229
230
	SELECT 2 >= 2;
231
        	-> 1
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
232
233
.. _equal:
234
235
EQUAL
236
-----
237
2194.5.2 by Andrew Hutchings
Fix SQL markup
238
Equal:
239
240
.. code-block:: mysql
1994.4.95 by Marisa Plumb
more comparison operators
241
242
	SELECT 1 = 0;
243
        	-> 0
244
	SELECT '0' = 0;
245
        	-> 1
246
	SELECT '0.0' = 0;
247
       		-> 1
248
	SELECT '0.01' = 0;
249
        	-> 0
250
	SELECT '.01' = 0.01;
251
        	-> 1
252
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
253
254
.. _not_equal:
255
256
NOT EQUAL
257
----------
258
2194.5.2 by Andrew Hutchings
Fix SQL markup
259
Not equal:
260
261
.. code-block:: mysql
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
262
1994.4.95 by Marisa Plumb
more comparison operators
263
	SELECT '.01' <> '0.01';
264
        	-> 1
265
	SELECT .01 <> '0.01';
266
        	-> 0
1994.4.96 by Marisa Plumb
position updates
267
	SELECT 'zing' <> 'zingg';
1994.4.95 by Marisa Plumb
more comparison operators
268
        	-> 1
1994.4.86 by Marisa Plumb
insert -- added multirow insert explanation and example
269