MySQLDQL之常见函数
Posted leeqico
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQLDQL之常见函数相关的知识,希望对你有一定的参考价值。
概念:类似于java的方法,将一组逻辑语句封装在方法体中,对外暴露方法名
好处:1、隐藏了实现细节 2、提高代码的重用性
调用:select 函数名(实参列表) 【from 表】;
1、字符函数
1.1、length 获取参数值的字节个数
SELECT LENGTH(‘john‘);
1.2、concat 拼接字符串
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-d52a53c0309f031a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.3、upper、lower
示例:将姓变大写,名变小写,然后拼接
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-40331396ea57d72f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.4、substr、substring
注意:索引从1开始
示例:截取从指定索引处后面所有字符
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-a2d04468301b7759.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
示例:截取从指定索引处指定字符长度的字符
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-c279c27329f524f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
案例:姓名中首字符大写,其他字符小写然后用_拼接,显示出来
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-31f943dd0eeb0e27.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.5、instr
返回子串第一次出现的索引,如果找不到返回0
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-cd97dc0a52cf70ee.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.6、trim
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-f7034a148e098320.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-6b252a2845f522e6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.7、lpad
用指定的字符实现左填充指定长度
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-9134acf14a4bdaa1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-94f8c4bc7162d69a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.8、rpad
用指定的字符实现右填充指定长度
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-7d905b69d10bbefa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
1.9、replace 替换
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-ca7872647f9d313c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2、数学函数
2.1、round 四舍五入
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-0907df9b57432daa.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.2、ceil 向上取整,返回>=该参数的最小整数
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-f3f8a759172dc620.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.3、floor 向下取整,返回<=该参数的最大整数
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-a0ff65655c3822a7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.4、truncate 截断
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-168013fe60b91e9c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.5、mod取余
mod(a,b) : a-a/b*b
mod(-10,-3):-10- (-10)/(-3)*(-3)=-1
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-503038c1228ef917.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3、日期函数
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-3199696282a61f54.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.1、now 返回当前系统日期+时间
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-2fbe1694cda001c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.2、curdate 返回当前系统日期,不包含时间
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-5deda1a0529d4859.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.3、curtime 返回当前时间,不包含日期
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-645a1ff9a7adb111.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.4、可以获取指定的部分,年、月、日、小时、分钟、秒
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-e14f14c808439592.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-74df33a03b698e19.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.5、str_to_date
将字符通过指定的格式转换成日期
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-ae47f99a49f7291c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.6、date_format
将日期转换成字符
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-d298fa5ef053ea7f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
4、其他函数
SELECT VERSION();
SELECT DATABASE();
SELECT USER();
5、流程控制函数
5.1、if函数
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-e4d8ea3d48c148c9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
5.2、case函数
方式一:
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-9e0c9bcc4819b268.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
方式二:
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-80272f565cff22f3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6、分组函数
功能:用作统计使用,又称为聚合函数或统计函数或组函数
分类:sum 求和、avg 平均值、max 最大值 、min 最小值 、count 计算个数
特点:
(1)sum、avg一般用于处理数值型
max、min、count可以处理任何类型
(2)以上分组函数都忽略null值
(3)可以和distinct搭配实现去重的运算
(4)count函数:一般使用count(*)用作统计行数
(5)和分组函数一同查询的字段要求是group by后的字段
6.1、简单使用
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-f43fb6bf702876e4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6.2、是否忽略null
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-728005e26bb9ef87.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6.3、和distinct搭配
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-3970241d3fc88184.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
6.4、count函数的详细介绍
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-b56b4a90c4c23061.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
效率:
MYISAM存储引擎下 ,COUNT(*)的效率高
INNODB存储引擎下,COUNT(*)和COUNT(1)的效率差不多,比COUNT(字段)要高一些
关注公众号:Java后端生活,干货文章第一时间送达!
![技术分享图片](https://upload-images.jianshu.io/upload_images/2058461-68c608a77a25c984.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
以上是关于MySQLDQL之常见函数的主要内容,如果未能解决你的问题,请参考以下文章