MySQL基础2——时间,日期

1. 获取日期与时间

MySQL保存日期的格式为 CCYY-MM-DD
datetime日期与时间 CCYY-MM-DD HH:MM:SS 占8字节 范围是1000-01-01 00:00:00~9999-12-31 23:59:59
date CCYY-MM-DD 占3个字节
year 占1个字节 year的取值范围是[1901,2155]
time 占3个字节 格式HH:MM:SS

mysql> select curdate();//获取当前日期
+------------+
| curdate()  |
+------------+
| 2016-08-16 |
+------------+
1 row in set (0.02 sec)

mysql> select curtime();//获取当前时间
+-----------+
| curtime() |
+-----------+
| 18:35:04  |
+-----------+
1 row in set (0.06 sec)

mysql> select now();//获取当前的日期与时间
+---------------------+
| 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条数据
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后的女同学
+----+--------+-----+------------+
| 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)

你可能感兴趣的:(MySQL基础2——时间,日期)