mysql length函数null_Mysql之常见函数

一、定义

类似于编程语言的方法,将一组逻辑语句封装在方法体中,对外暴露方法名。

好处:1.隐藏了实现细节 2.提高代码重用性

调用:select 函数名(实参列表)【from 表】

分类:

1.单行函数,如concat,length,ifnull等

2.分组函数:做统计使用,又叫统计函数,聚合函数,组函数

二、字符函数

#LENGTH(str) 获取参数值的字节数

select length('mark') //4

select length('中国') //6 utf-8 中文占3个字符

#CONCAT(str1,str2,...) 拼接字符串

select CONCAT('aa','bb') // aabb

#UPPER(str) 大写、LOWER(str) 小写

select CONCAT(UPPER('aaa'),LOWER('BBB')) // AAAbbb

#SUBSTR(str FROM pos FOR len)、SUBSTRING(str FROM pos FOR len)

#索引从1开始

select SUBSTR('唐吉坷德多佛朗明哥', 5) // 截取从指定索引处到最后

select SUBSTR('唐吉坷德多佛朗明哥', 1, 4) // 截取从指定索引处,指定字符长度

#INSTR(str,substr) 返回子串第一次出现的索引,找不到返回0

select instr('剑豪世界第一大剑豪', '剑豪') // 1

#TRIM([remstr FROM] str)

select trim(' 任我行 ') // 任我行

select trim('a' from 'aaaaaa任我aa行aaaaa') // 任我aa行

#LPAD(str,len,padstr) 用指定字符实现左填充长度

select lpad('张三丰', 10, 'a') // aaaaaaa张三丰

#RPAD(str,len,padstr) 用指定字符实现左填充长度

select rpad('张三丰', 10, 'a') // 张三丰aaaaaaa

#REPLACE(str,from_str,to_str)

select replace('日月神教教主任我行', '任我行', '东方不败') // 日月神教教主东方不败

三、数学函数

# ROUND(X) 四舍五入

select round(4.5) // 5

select round(-4.5) // -5

select round(-4.555, 2) // -4.56 小数点后保存两位

#CEIL(X) 向上取整

select ceil(1.01) // 2

select ceil(-1.01) // -1

#FLOOR(X) 向下取整

select floor(-1.01) // -2

#`TRUNCATE`(X,D) 截断

select truncate(1.69, 1) // 1.6

#mod 取余

mod(a,b) // a-a/b*b 除号两边是整数,结果取整

select MOD(-10, -3) // -1

四、日期函数

#NOW() 当前系统日期+时间

select now() // 2019-10-29 15:07:31

#CURDATE() 当前系统日期

select curdate() // 2019-10-29

#CURTIME() 当前时间 不含日期

select curtime() // 15:09:14

#返回指定部分,年、月、日、小时、分钟、秒

select YEAR('2019-01-01') // 2019

select MONTH('2019-01-01') // 1

#STR_TO_DATE(str,format) 将日期格式字符转换为指定日期

select str_to_date('3-4-2019', '%d-%c-%Y') // 2019-04-03

#DATE_FORMAT(date,format) 将日期转换为字符

select date_format(now(), '%y年%m月%d日') // 19年10月29日

五、其他函数

select version()

select database()

select user()

六、流程控制函数

select if(10 > 5, '大', '小') // 大

#case函数使用一: switch case 的效果

/*

case 要判断的字段或表达式

when 常量值1 then 要显示的值1或语句1;

when 常量值2 then 要显示的值2或语句2;

...

else 要显示的值n或语句n;

end

*/

select salary 原始工资,department_id,

case department_id

when 30 then salary*1.1

when 40 then salary*1.2

when 50 then salary*1.3

else salary

end as 新工资

from employees

#case函数使用二: 多重if

/*

case

when 条件1 then 要显示的值1或语句1;

when 条件2 then 要显示的值2或语句2;

...

else 要显示的值n或语句n;

end

*/

select salary, case

when salary > 20000 then 'A'

when salary > 15000 then 'B'

when salary > 10000 then 'C'

else 'D'

END as 工资级别

from employees

七、分组函数

# 简单使用

select sum(salary) from employees // 工资之和

select avg(salary) from employees // 平均工资

select max(salary) from employees // 最大工资

select min(salary) from employees // 最小工资

select count(salary) from employees // salary 字段非空有几个

# 参数类型

sum(),avg() 处理数值型

max(),min(),count() 可处理任何类型

以上分组函数都忽略null值

和分组函数一起查询的字段要求是group by 后的字段

select count(*) from employees // 一般用这个

select count(1) from employees // 性能和上一个差不多

select count(salary) from employees //salary 为null的数据不统计

# 和distinct搭配

select sum(distinct salary), sum(salary) from employees

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值