【MySQL】MySQL内置函数--日期函数/字符串函数/数学函数/其他相关函数

06-01 1666阅读

文章目录

    • 1.日期函数
    • 2.字符串函数
    • 3.数学函数
    • 4.其它函数

1.日期函数

MySQL中内置了一下函数:

函数名称 描述
current_date() 当前日期
current_time() 当前时间
current_timestamp() 当前时间戳
date(datetime) 返回datetime参数的日期部分
date_add(date,interval d_value_type) 在date中添加日期或时间,interval后的数值单位可以是year minute second day
date_sub(date,interval d_value_type) 在date中减去日期或时间,interval后的数值单位可以是year minute second day
datediff(date1,date2) 两个日期的差,单位是天
now() 当前日期时间

获得年月日

mysql> select current_date();
+----------------+
| current_date() |
+----------------+
| 2023-11-11     |
+----------------+
1 row in set (0.00 sec)

获得时分秒

mysql> select current_time();
+----------------+
| current_time() |
+----------------+
| 15:46:47       |
+----------------+
1 row in set (0.00 sec)

获得时间戳

mysql> select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2023-11-11 15:47:19 |
+---------------------+
1 row in set (0.00 sec)

在日期的基础上加日期

mysql> select date_add('2023-11-11',interval 10 day);
+----------------------------------------+
| date_add('2023-11-11',interval 10 day) |
+----------------------------------------+
| 2023-11-21                             |
+----------------------------------------+
1 row in set (0.01 sec)

在日期的基础上减去时间

mysql> select date_sub('2023-11-11',interval 10 day);
+----------------------------------------+
| date_sub('2023-11-11',interval 10 day) |
+----------------------------------------+
| 2023-11-01                             |
+----------------------------------------+
1 row in set (0.00 sec)

计算两个日期之间相差多少天

mysql> select datediff('2023-11-11','2023-10-21');
+-------------------------------------+
| dat
【MySQL】MySQL内置函数--日期函数/字符串函数/数学函数/其他相关函数
(图片来源网络,侵删)
【MySQL】MySQL内置函数--日期函数/字符串函数/数学函数/其他相关函数
(图片来源网络,侵删)
【MySQL】MySQL内置函数--日期函数/字符串函数/数学函数/其他相关函数
(图片来源网络,侵删)
免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们。

目录[+]

取消
微信二维码
微信二维码
支付宝二维码