Oracle 5.0 Reference Manual page 227

Table of Contents

Advertisement

Finding animals with birthdays in the upcoming month is also simple. Suppose that the current month is
April. Then the month value is
mysql>
SELECT name, birth FROM pet WHERE MONTH(birth) = 5;
+-------+------------+
| name
| birth
+-------+------------+
| Buffy | 1989-05-13 |
+-------+------------+
There is a small complication if the current month is December. You cannot merely add one to the
month number (12) and look for animals born in month 13, because there is no such month. Instead,
you look for animals born in January (month 1).
You can write the query so that it works no matter what the current month is, so that you do not have
to use the number for a particular month.
a given date. If you add a month to the value of
[925], the result produces the month in which to look for birthdays:
MONTH()
mysql>
SELECT name, birth FROM pet
->
WHERE MONTH(birth) = MONTH(DATE_ADD(CURDATE(),INTERVAL 1 MONTH));
A different way to accomplish the same task is to add
using the modulo function (MOD) to wrap the month value to
mysql>
SELECT name, birth FROM pet
->
WHERE MONTH(birth) = MOD(MONTH(CURDATE()), 12) + 1;
[925]
MONTH()
number between
from November (11) to January (1).
3.3.4.6. Working with
The
value can be surprising until you get used to it. Conceptually,
NULL
unknown value" and it is treated somewhat differently from other values.
To test for NULL, use the
mysql>
SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
|
0 |
+-----------+---------------+
You cannot use arithmetic comparison operators such as
NULL. To demonstrate this for yourself, try the following query:
mysql>
SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
|
NULL |
+----------+-----------+----------+----------+
Because the result of any arithmetic comparison with
meaningful results from such comparisons.
In MySQL,
0
boolean operation is 1.
This special treatment of
animals are no longer alive using
Two
values are regarded as equal in a
NULL
Retrieving Information from a Table
and you can look for animals born in May (month 5) like this:
4
|
returns a number between
and 11. So the addition has to be after the
0
Values
NULL
[877]
IS NULL
1 |
NULL |
NULL |
or
means false and anything else means true. The default truth value from a
NULL
is why, in the previous section, it was necessary to determine which
NULL
death IS NOT NULL
[919]
enables you to add a time interval to
DATE_ADD()
[918], then extract the month part with
CURDATE()
to get the next month after the current one after
1
if it is currently 12:
0
and 12. And
1
MOD(something,12)
MOD()
and
[877]
IS NOT NULL
[875],
=
NULL |
is also NULL, you cannot obtain any
NULL
instead of
GROUP
BY.
207
[911]
returns a
[911], otherwise we would go
means "a missing
NULL
operators, as shown here:
[876], or
[876]
to test for
<
<>
death <>
NULL.

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents