取得当天:
mysql> SELECT curdate();
+------------+
| curdate() |
+------------+
| 2018-11-24 |
+------------+
取得当前日期:
mysql> select now();
+---------------------+
| now() |
+---------------------+
| 2018-11-24 13:41:51 |
+---------------------+
取得前一天:
mysql> select date_sub(curdate(),interval 1 day);
+------------------------------------+
| date_sub(curdate(),interval 1 day) |
+------------------------------------+
| 2018-11-23 |
+------------------------------------+
括号中为当天时间的前一天,如果统计前几天就将括号中的’1’改成相应的天数。如果要算月或年,直接将day改为month或year即可
SELECT YEAR(DATE_SUB(CURDATE(),INTERVAL 1 DAY));
+------------------------------------------+
| YEAR(DATE_SUB(CURDATE(),INTERVAL 1 DAY)) |
+---------------------------------------