~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
Logical and Comparison Operators
================================

==============================     ================================
Operators                           Description
==============================     ================================
:ref:`and` &&                       Logical AND
:ref:`or` (||)                      Logical OR
:ref:`xor` 	                    Logical XOR
:ref:`not` (!)                      Logical NOT
:ref:`less_than` <      	    less than
:ref:`greater_than` >   	    greater than
:ref:`less_or_equal` <=             less than or equal to
:ref:`greater_or_equal` >=          greater than or equal to
:ref:`equal` =  	            equal
:ref:`not_equal` <> or !=           not equal
==============================     ================================


.. _and:

AND
---

This logical operator AND:

* evaluates to 1 if all operands are nonzero and not NULL
* evaluates to 0 if one or more operands are 0 
* otherwise returns NULL 

.. code-block:: mysql

	SELECT 1 && 1;
	        -> 1
	SELECT 1 && 0;
        	-> 0
	SELECT 1 && NULL;
        	-> NULL
	SELECT 0 && NULL;
        	-> 0
	SELECT NULL && 0;
        	-> 0

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:

.. code-block:: mysql

	SELECT first_name, last_name, age
	FROM user_details
	WHERE age >= 20 AND age <= 25;

The output would be:

+---------------+------------------+-------+
|first_name 	|last_name 	   |age    |
+===============+==================+=======+
|Mary 	        |Bean   	   |20     |
+---------------+------------------+-------+
|Henry  	|Catson 	   |22     |
+---------------+------------------+-------+
|Sheila 	|Donaldson         |25     |
+---------------+------------------+-------+  

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.

.. _or:

OR
--

This logical operator OR:

* returns 1 if any operand is nonzero and both operands are non-NULL, and returns 0 otherwise
* returns 1 with a NULL operand if the other operand is nonzero, and retunes NULL otherwise
* returns NULL if both operands are NULL

.. code-block:: mysql

	SELECT 1 || 1;
        	-> 1
	SELECT 1 || 0;
        	-> 1
	SELECT 0 || 0;
        	-> 0
	SELECT 1 || NULL;
        	-> 1
	SELECT 0 || NULL;
        	-> NULL

In other words, OR is used to select rows that satisfy at least one of the given conditions.

For example, the following query could be used to find the user_id for people that live in either California or Texas:

.. code-block:: mysql

	SELECT user_id, state
	FROM user_location
	WHERE state = 'California' OR state = 'Texas';

The result set could be something like:

+---------------+------------------+-----------+
|user_id 	|city   	   |state      |
+===============+==================+===========+
|608            |Sacremento   	   |California |
+---------------+------------------+-----------+
|844     	|Austin 	   |Texas      |
+---------------+------------------+-----------+
|917    	|Oakland           |California |
+---------------+------------------+-----------+  


.. _xor:

XOR
---

* returns NULL if either operand is NULL
* evaluates to 1 for non-NULL operands (if an odd number of operands is nonzero)
* otherwise 0 is returned

.. code-block:: mysql

	SELECT 1 XOR NULL;
        	-> NULL
	SELECT 1 XOR 0;
        	-> 1
	SELECT 1 XOR 1 XOR 1;
        	-> 1
	SELECT 1 XOR 1;
        	-> 0

Note that "a XOR b" is the mathematical equivalent of (a AND (NOT b)) OR ((NOT a) and b). 


.. _not:

NOT
---

This logical operator NOT:

* evaluates to 1 if the operand is 0
* evaluates to 0 if the operand is nonzero
* NOT NULL returns NULL

.. code-block:: mysql

	SELECT NOT 10;
        	-> 0
	SELECT NOT 0;
        	-> 1
	SELECT NOT NULL;
        	-> NULL
	SELECT ! (1+1);
        	-> 0
	SELECT ! 1+1;
        	-> 1

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.

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:

.. code-block:: mysql

	SELECT user_id, title, occupation
	FROM user_occupations
	WHERE NOT occupation = 'Doctor';

The result set would be something like:

+---------------+------------------+--------------+
|user_id 	|degree   	   |occupation    |
+===============+==================+==============+
|322            |PhD    	   |Professor     |
+---------------+------------------+--------------+
|579     	|PhD    	   |Writer        |
+---------------+------------------+--------------+
|681     	|MD                |Consultant    |
+---------------+------------------+--------------+  


.. _less_than:

LESS THAN
----------

Less than:

.. code-block:: mysql

	SELECT 2 < 2;
        	-> 0


.. _greater_than:

GREATER THAN
-------------

Greater than:

.. code-block:: mysql

	SELECT 2 > 2;
        	-> 0

.. _less_or_equal:

LESS THAN OR EQUAL
-------------------

Less than or equal:

.. code-block:: mysql

	SELECT 0.1 <= 2;
        	-> 1


.. _greater_or_equal:

GREATER THAN OR EQUAL
----------------------

Greater than or equal:

.. code-block:: mysql

	SELECT 2 >= 2;
        	-> 1

.. _equal:

EQUAL
-----

Equal:

.. code-block:: mysql

	SELECT 1 = 0;
        	-> 0
	SELECT '0' = 0;
        	-> 1
	SELECT '0.0' = 0;
       		-> 1
	SELECT '0.01' = 0;
        	-> 0
	SELECT '.01' = 0.01;
        	-> 1


.. _not_equal:

NOT EQUAL
----------

Not equal:

.. code-block:: mysql

	SELECT '.01' <> '0.01';
        	-> 1
	SELECT .01 <> '0.01';
        	-> 0
	SELECT 'zing' <> 'zingg';
        	-> 1