MySQL-获取当天时间、前几天时间函数

取得当天:

SELECT curdate();

mysql> SELECT curdate();
+------------+
| curdate()  |
+------------+
| 2013-07-29 |
+------------+

 

取得当前日期:

mysql> select now();
+---------------------+
| now()               |
+---------------------+
| 2013-07-29 22:10:40 |
+---------------------+

取得前一天:

mysql> select date_sub(curdate(),interval 1 day);
+------------------------------------+
| date_sub(curdate(),interval 1 day) |
+------------------------------------+
| 2013-07-28                         |
+------------------------------------+

 

括号中为当天时间的前一天,如果统计前几天就将括号中的’1’改成相应的天数。如果要算月或年,直接将day改为month或year即可

 

 取得前一天的年份:

mysql> SELECT YEAR(DATE_SUB(CURDATE(),INTERVAL 1 DAY));
+------------------------------------------+
| YEAR(DATE_SUB(CURDATE(),INTERVAL 1 DAY)) |
+------------------------------------------+
|         2013              |
+------------------------------------------+

 

你可能感兴趣的:(MYSQL)