MySQL常用函数实例

聚集函数

avg()
count(age)       填了年龄的数量
max(age)
min(age)
sum(age)

select avg(age) as "年龄平均值" from student 
where gender="male";

用于处理字符串的函数

合并字符串函数:concat(str1,str2,str3)
比较字符串大小函数:strcmp("a","b")    --1
获取字符串字节数函数:length("abcd")   --4
获取字符串字符数函数:char_length("abcd")  --4
字母大小写转换函数:大写:upper("a"),ucase("x");
小写lower("abc"), lcase("x")

分隔
select concat-_ws("i","am","man");  --i am man


字符串查找函数:
find_in_set("abc""123,456,abc"); --3
findld("abc""123","456","abc");  --3

返回子串str1在字符串str2中的位置:
locate ("a","123a456")     --4
position("a" in "123a123")  --4
instr("123a123","a")    --4

获取指定位置的子串:
elt(1,"a","b","c")    --a
left("man",2)    --ma
right("man",1)  --n
substring("iamman",2,5)  --anman

字符串去空函数:
ltrim("     hello wrold!")    --"hello world!"
rtrim(" hello   ")         --hello
trim("     hello world        ")      

字符串替换函数:
insert("hello wrold",7,5,"jie")
replace("hello admin","admin","jie")

用于处理数值的函数

绝对值函数:abs(x)
向上取整函数:ceil(x)
向下取整函数:floor(x)
取模函数:mod(x,y)
随机数函数:rand(1)
四舍五入函数:round("3.1415926",3)   --3.142
数值截取函数:truncate("3.1415926",3)  --3.141

用于处理时间日期的函数

获取当前日期:curdate()current_date()

获取当前时间:curtime()current_time()

获取当前日期时间:select now();

从日期中选择出月份数:month(date),monthname(date)
从日期中选择出周数:week('2022-02-02 11:11:11')
从日期中选择出周数:year(date)
从时间中选择出小时数:hour(time)
从时间中选择出分钟数:minute(time)
从时间中选择出今天是周几:weekday(date),dayname(date)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Gary jie

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

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

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

打赏作者

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

抵扣说明:

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

余额充值