获取当天日期
SELECT curdate();
mysql> SELECT curdate();
+------------+
| curdate() |
+------------+
| 2016-04-01 |
+------------+
取得当前日期与时间
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2016-04-01 10:10:40 |
+---------------------+
取得前一天
mysql> select date_sub(curdate(),interval 1 day);
+------------------------------------+
| date_sub(curdate(),interval 1 day) |
+------------------------------------+
| 2016-04-01 |
+------------------------------------+
如果统计前几天就将括号中的1
改成相应的天数即可。如果要算月或年,直接将day
改为month
或year
即可。
取得前一天的年份
mysql> SELECT YEAR(DATE_SUB(CURDATE(),INTERVAL 1 DAY));
+------------------------------------------+
| YEAR(DATE_SUB(CURDATE(),INTERVAL 1 DAY)) |
+------------------------------------------+
| 2016 |
+------------------------------------------+
网友评论