1. 獲取日期與時間
MySQL保存日期的格式為 CCYY-MM-DD
datetime日期與時間 CCYY-MM-DD HH:MM:SS 占8字節(jié) 范圍是1000-01-01 00:00:00~9999-12-31 23:59:59
date CCYY-MM-DD 占3個字節(jié)
year 占1個字節(jié) year的取值范圍是[1901,2155]
time 占3個字節(jié) 格式HH:MM:SS
mysql> select curdate();//獲取當(dāng)前日期
+------------+
| curdate() |
+------------+
| 2016-08-16 |
+------------+
1 row in set (0.02 sec)
mysql> select curtime();//獲取當(dāng)前時間
+-----------+
| curtime() |
+-----------+
| 18:35:04 |
+-----------+
1 row in set (0.06 sec)
mysql> select now();//獲取當(dāng)前的日期與時間
+---------------------+
| now() |
+---------------------+
| 2016-08-16 18:35:13 |
+---------------------+
1 row in set (0.00 sec)
mysql> select * from stu;
+----+--------+-----+------------+
| id | name | sex | birthday |
+----+--------+-----+------------+
| 1 | Lucy | 0 | 1990-01-01 |
| 2 | LiLy | 0 | 1990-01-10 |
| 3 | 李雷 | 1 | 1991-03-06 |
| 4 | 韓梅梅 | 0 | 1992-11-12 |
+----+--------+-----+------------+
4 rows in set (0.00 sec)
mysql> insert into stu(name,sex,birthday)values('范冰冰',0,'1980-02-26'),('朱元璋',1,'1322-09-11');//往表中插入2條數(shù)據(jù)
Query OK, 2 rows affected (0.12 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from stu;
+----+--------+-----+------------+
| id | name | sex | birthday |
+----+--------+-----+------------+
| 1 | Lucy | 0 | 1990-01-01 |
| 2 | LiLy | 0 | 1990-01-10 |
| 3 | 李雷 | 1 | 1991-03-06 |
| 4 | 韓梅梅 | 0 | 1992-11-12 |
| 5 | 范冰冰 | 0 | 1980-02-26 |
| 6 | 朱元璋 | 1 | 1322-09-11 |
+----+--------+-----+------------+
6 rows in set (0.00 sec)
mysql> select*from stu where sex=0 and birthday between 19900101 and 19991231;//查找所有90后的女同學(xué)
+----+--------+-----+------------+
| id | name | sex | birthday |
+----+--------+-----+------------+
| 1 | Lucy | 0 | 1990-01-01 |
| 2 | LiLy | 0 | 1990-01-10 |
| 4 | 韓梅梅 | 0 | 1992-11-12 |
+----+--------+-----+------------+
3 rows in set (0.03 sec)