|
|
MySQL development tutorial - MySQL Date column type part 1Till now we've dealt with text (varchar) and numbers (int) data types. To understand date type, we'll create one more table. Click here for viewing this new table, employee_per. The details of the table can be displayed with DESCRIBE command.
mysql> DESCRIBE employee_per;
+------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+-------+
| e_id | int(10) unsigned | | PRI | 0 | |
| address | varchar(60) | YES | | NULL | |
| phone | int(11) | YES | | NULL | |
| p_email | varchar(60) | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| sex | enum('M','F') | YES | | NULL | |
| m_status | enum('Y','N') | YES | | NULL | |
| s_name | varchar(40) | YES | | NULL | |
| children | int(11) | YES | | NULL | |
+------------+------------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
Notice that column birth_date has date as column type. I've also introduced another column type ENUM, which we'll discuss later. Characteristics of Date Operations on Date Using = and != operators select p_email, phone from employee_per where birth_date = '1969-12-31'; +---------------------------+---------+ | p_email | phone | +---------------------------+---------+ | killeratlarge@elmmail.com | 6666666 | +---------------------------+---------+ 1 row in set (0.00 sec) Note: MySQL requires the dates to be enclosed in quotes. Using >= and <= operators select e_id, birth_date from employee_per where birth_date >= '1970-01-01'; +------+------------+ | e_id | birth_date | +------+------------+ | 1 | 1972-03-16 | | 4 | 1972-08-09 | | 5 | 1974-10-13 | | 8 | 1975-01-12 | | 17 | 1970-04-18 | | 18 | 1973-10-09 | | 19 | 1973-01-20 | +------+------------+ 7 rows in set (0.00 sec) Specifying ranges select e_id, birth_date from employee_per where birth_date BETWEEN '1969-01-01' AND '1974-01-01'; +------+------------+ | e_id | birth_date | +------+------------+ | 1 | 1972-03-16 | | 4 | 1972-08-09 | | 6 | 1969-12-31 | | 17 | 1970-04-18 | | 18 | 1973-10-09 | | 19 | 1973-01-20 | +------+------------+ 6 rows in set (0.00 sec) select e_id, birth_date from employee_per where birth_date >= '1969-01-01' AND birth_date <= '1974-01-01'; +------+------------+ | e_id | birth_date | +------+------------+ | 1 | 1972-03-16 | | 4 | 1972-08-09 | | 6 | 1969-12-31 | | 17 | 1970-04-18 | | 18 | 1973-10-09 | | 19 | 1973-01-20 | +------+------------+ 6 rows in set (0.00 sec)
Page contents: Mysql development tutorial - mysql date column type - mysql column types
Page URL: http://www.webdevelopersnotes.com/tutorials/sql/ mysql_development_tutorial_mysql_date_column_type_part_1.php3
|
|