MySQL常用函数

1.concat(value1,value2,value3)

作用:字符串拼接

注意:字符串和数字可以直接拼接

2.convert(column,type)

作用:将一个列取出的数据类型转换成目前需要的数据类型

3.cast(column as type)

作用:将一个列取出的数据类型转换成目前需要的数据类型

4.使用now()

作用:求当前日期(年月日,时分秒,格式如:2024-09-19 16:29:53

5.使用date_sub(column,interval num unit)

作用:求一个日期的前/后几年/月/日,

num为正,所求只为后几年/月/日

num为负,所求只为前几年/月/日

unit:mysql中的时间单位:YAEY,MONTH,DAY

6.date_format(column,patten)

作用:将需要的时间进行格式化

注意:patten需要带上单引号

格式控制符

描述

%Y

年份,四位数字

%y

年份,两位数字

%m

月份,两位数字

%c

月份,没有前导零

%d

月份中的第几天,两位数字

%e

月份中的第几天,没有前导零

%H

小时,24小时制,两位数字

%h

小时,12小时制,两位数字

%i

分钟,两位数字

%s

秒钟,两位数字

%p

AM 或 PM

事例:

SQL如下:

select concat('ads',123) as result,now() as 'mysql数据库获取目前时间',convert(now(),date) as '使用convert转换格式后年月日',

CAST(now() as date) as '使用cast对当前时间进行格式化',date_sub(now(),interval 1 day) as '使用date_sub求当前日期前一天日期',

date_sub(now(),interval 1 month) as '使用date_sub求当前日期前一月日期',date_sub(now(),interval 1 year) as '使用date_sub求当前日期前一年日期',

date_sub(now(),interval -1 day) as '使用date_sub求当前日期后一天日期',date_format(now(),'%Y-%m') as '使用date_format将时间格式修改为:年月',

date_format(now(),'%Y') as '使用date_format将时间格式修改为:年',date_format(now(),'%Y-%m-%d') as '使用date_format将时间格式修改为:年月日'

//输出结果:

{

  "result": "ads123",

  "mysql数据库获取目前时间": "2024-09-19 16:40:45",

  "使用date_sub求当前日期前一天日期": "2024-09-18 16:40:45",

  "使用date_sub求当前日期前一月日期": "2024-08-19 16:40:45",

  "使用date_format将时间格式修改为:年月日": "2024-09-19",

  "使用cast对当前时间进行格式化": "2024-09-19",

  "使用date_format将时间格式修改为:年": "2024",

  "使用convert转换格式后年月日": "2024-09-19",

  "使用date_sub求当前日期前一年日期": "2023-09-19 16:40:45",

  "使用date_format将时间格式修改为:年月": "2024-09",

  "使用date_sub求当前日期后一天日期": "2024-09-20 16:40:45",

  "使用date_format将时间格式修改为年": "2024",

  "使用date_format将时间格式修改为:年月日": "2024-09-19",

  "使用date_format将时间格式修改为:年月": "2024-09"

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

莳光.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值