MySQL常用函数【随时更新】

一  日期函数

1. 取本月1号

mysql> select concat(date_format(now(), '%Y-%m'),'-01');
+-------------------------------------------+
| concat(date_format(now(), '%Y-%m'),'-01') |
+-------------------------------------------+
| 2020-09-01                                |
+-------------------------------------------+
1 row in set (0.02 sec)

mysql> select concat(date_format(now(), '%Y%m'),'01');
+-----------------------------------------+
| concat(date_format(now(), '%Y%m'),'01') |
+-----------------------------------------+
| 20200901                                |
+-----------------------------------------+
1 row in set (0.02 sec)

2. 取本周始末

mysql> select subdate(now(), date_format(now(),'%w')-1) '本周一';
+---------------------+
| 本周一              |
+---------------------+
| 2020-09-14 17:10:03 |
+---------------------+
1 row in set (0.04 sec)

mysql> select subdate(now(), date_format(now(),'%w')-7) '本周日';
+---------------------+
| 本周日              |
+---------------------+
| 2020-09-20 17:10:12 |
+---------------------+
1 row in set (0.05 sec)

原文地址:https://www.cnblogs.com/LUA123/p/13686210.html