MySQL-常用函数大全

MySQL函数大全

MySQL-verison:5.7.17

MySQL常用函数分类

  • 数学函数
  • 聚合函数
  • 字符串函数
  • 日期时间函数
数学函数
-- 绝对值
select abs(-123)		=>	123 
-- 平方根
select sqrt(4)			=>	2
-- 取余数
select mod(5,3)			=>	2
-- 向上取整
select ceil(1.111)		=>	2
-- 向下取整
selct floor(1.999)		=>	1
-- 四舍五入
select round(1.345)		=>	1		默认保留整数
select round(1.345,2)	=>	1.35	2为精度
-- 返回参数的符号
select sign(-10)		=>	-1
select sign(10)			=>	1
-- 幂运算
select pow(3,2)			=>	9
-- 随机数
select rand();			=>	0.9057697559760601	(0~1之间)
-- 截取指定小数位数
select truncate(2.783,2)=> 2.78
-- 返回集合中的最大的值
select greatest(10203040)	=>	40
-- 返回集合中的最小的值
select least(10203040)		=>	10
聚合函数
 - sum(FIELD)		求和		应用于数值类型/日期	忽略NULL
 - avg(FIELD)		求平均值	应用于数值类型/日期	忽略NULL
 - max(FIELD)		求最大值	应用于数值类型/日期	忽略NULL
 - min(FIELD)		求最小值	应用于数值类型/日期	忽略NULL
 - count(FIELD)		计数		应用于一切类型		忽略NULL
 	count(1)				是由一列
 	count(*)		所有行	有主键,多列
 	count(FIELD) 	排除NULL

字符串函数

-- 字符串字节长度
select length('a我c')=>	5		utf8mb4 三字节汉字
-- 字符串字符长度
select character_length('a我c'); => 3
-- 位置替换
select insert('FootBall',1,4,'Basket'); => BasktBall -- 1和4为我位置,从1开始
-- 内容替换
select replace('123abc123','123','456'); => 456abc456
-- 左截取
select left('henrychen',5);	=>	henry	-- 5表示数量
-- 右截取
select right('henrychen',4)	=>	chen	-- 4表示数量
-- 截取
select mid('henrychen',4);	=>	chen	-- 6表示位置,从1开始,4表示数量
-- 转小写
select lower('ABC');	=>	abc
-- 转大写
select upper('abc');	=> ABC
-- 去两端空格
select trim('    abc    ');	=>	abc
-- 字符串反转
select reverse('abc');	=>	cba
日期时间函数

mysql中符合日期格式的字符串等同于日期

-- 创建日期
select date('2021-1-1');	=>	2021-1-1
-- 获取系统当前时间
select now();				=>	2021-05-13 11:20:21
-- 获取系统当前日期
select current_date();		=>	2021-05-13
-- 获取系统当前时间
select current_time();		=>	11:23:56
-- 获取当前系统时间戳
select unix_timestamp();		=> 1620876322
-- 格式化时间戳
select from_nixtime(1620876322);	=>	2021-05-13 11:25:22
-- 获取年份,返回值范围是1970~2069
select year (now());		=> 2021
-- 季
select quarter(now());		=> 2
-- 月
select month(now());		=> 5
-- 年周
select weekofyear(now());	=>	19
select week(now());
-- 月周
-- 年日
select dayofyear(now());	=>	123
--月日
select day(now());			=>	13
select dayofmonth(now());
--周几
select weekday(date('2021-5-9'));	=>	6	-- 周一~周日 0~6
select dayofweek(date('2021-5-9'));	=>	1	-- 周日~周六	1~7
select dayname(date('2021-5-9'));	=>	Sunday
流程控制
-- if(LOGIC_EXPRESSION,VALUE1,VALUE2)函数
select if(true,1,2);
-- 空值判断
select ifnull(NULL,1);		=>	1
select ifnull(2,1);			=>	2
-- 多分支
	-- switch..case
select (case v when 1 then v1 when 2 then v2 else v3 end) ALIAS;
	-- if...else
selec (case when v<=1 then v1 when v<=10 then v2 else v3 end) A;

MYSQL高阶函数

字符串函数扩展

字符串拼接

-- 行内拼接字符串
concat(F1,...,FN)

-- 行内拼接:指定分隔符拼接
concat_ws(sep,F1,...FN)

-- 分组Pinjie:组内拼接字符串,支持组内排序
group_concat(FIELD [order by FIELD ASC/DESC])	--	默认分隔符为,
group_concat(FIELD separator '_')	-- 指定分隔符

字符符串截取

-- 获取指定位置处的子字符串
substring(String,v1) 	-- 返回从v1开始到结尾的字符串,如果v1为负数,表示从倒数第|v1|个位置开始
substring(String,v1,v2)	-- 返回v1~v2之间的字符串

-- 根据分隔符提取字符串

 - delimiter 分隔符  count 第几个分隔符  count > 0 前几个分隔符  count < 0 后几个分隔符

substring_index(String,delimiter,count)

-- 提取第几个
substring_index(substring_index(String,delimiter,n),delimiter,-1)

字符串查找

-- 定位子字符串在父字符串中的位置
-- 从startPos(incluse)开始在string中找到substr首次出现的位置
- 如果 substr==null || string == null return null
- 如果 substr not in string retrun 0
- 否则返回1-char_length(String)
locate(substr,String[,startPos])

-- 返回substr在String中第一次出现的首字符的位置
instr(String,substr)

字符串函数应用:列转行,行转列
-列转行

列转行:聚合
select
	group_field,...,
	group_concat(FIELD/concat(FIELD,'SEP',...)) alias
where
group by
	group_field
having ...

-行转列

行转列:将一列拆成多行
select FIELD_A from tabA
union all
select FIELD_B from tabA

limit分页查询

-- limit offset,size;
select ... limit(pageNo-1)*pageSize,pageSize;
集合函数
-- 集合包含
find_in_set(FIELD/CONST,'v1,v2,...')

-- 分组:分组收集函数
collect_list(FIELD)	-- 将该列所有行收集成一个集合
collect_set(FIELD)	-- 将该列所有行收集成一个去重后的集合
日期函数扩展

– 以date为基础,增加指定unit(单位)指定num(数量)之后的日期
date_add/adddate(date,interval ±num unit)
select date_sub(current_date(), INTERVAL 1 DAY)
adddate(current_date(), 1)

– 以date为基础,减去指定的时间间隔
date_sub/subdate(date,interval ±num unit)
subdate(current_date(), 1)

– 时间加法运算,在原始时间上添加指定的时间
addtime(‘2018-10-31 23:59:59’,‘0:1:1’)

– 时间减法运算,在原始时间上减去指定的时间
subtime(‘2018-10-31 23:59:59’,‘0:1:1’)

– 获取两个日期之间间隔,返回参数1减去参数2的值
datediff(‘2017-11-30’,‘2017-11-29’)

– 格式化指定的日期,根据参数返回指定格式的值
date_format(date,format)
date_format(‘2017-11-15 21:45:00’,’%W %M %D %Y’)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MySQL中concat函数 使用方法: CONCAT(str1,str2,…) 返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,则返回值为 NULL。 注意: 如果所有参数均为非二进制字符串,则结果为非二进制字符串。 如果自变量中含有任一二进制字符串,则结果为一个二进制字符串。 一个数字参数被转化为与之相等的二进制字符串格式;若要避免这种情况,可使用显式类型 cast, 例如: SELECT CONCAT(CAST(int_col AS CHAR), char_col) MySQL的concat函数可以连接一个或者多个字符串,如 mysql> select concat('10'); +--------------+ | concat('10') | +--------------+ | 10 | +--------------+ 1 row in set (0.00 sec) mysql> select concat('11','22','33'); +------------------------+ | concat('11','22','33') | +------------------------+ | 112233 | +------------------------+ 1 row in set (0.00 sec) MySQL的concat函数在连接字符串的时候,只要其中一个是NULL,那么将返回NULL mysql> select concat('11','22',null); +------------------------+ | concat('11','22',null) | +------------------------+ | NULL | +------------------------+ 1 row in set (0.00 sec) MySQL中concat_ws函数 使用方法: CONCAT_WS(separator,str1,str2,...) CONCAT_WS() 代表 CONCAT With Separator ,是CONCAT()的特殊形式。第一个参数是其它参数的分隔符。分隔符的位置放在要连接的两个字符串之间。分隔符可以是一个字符串,也可以是其它参数。 注意: 如果分隔符为 NULL,则结果为 NULL。函数会忽略任何分隔符参数后的 NULL 值。 如连接后以逗号分隔 mysql> select concat_ws(',','11','22','33'); +-------------------------------+ | concat_ws(',','11','22','33') | +-------------------------------+ | 11,22,33 | +-------------------------------+ 1 row in set (0.00 sec) 和MySQL中concat函数不同的是, concat_ws函数在执行的时候,不会因为NULL值而返回NULL mysql> select concat_ws(',','11','22',NULL); +-------------------------------+ | concat_ws(',','11','22',NULL) | +-------------------------------+ | 11,22 | +-------------------------------+ 1 row in set (0.00 sec) MySQL中group_concat函数 完整的语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符']) 基本查询 mysql> select * from aa; +------+------+ | id| name | +------+------+ |1 | 10| |1 | 20| |1 | 20| |2 | 20| |3 | 200 | |3 | 500 | +------+------+ 6 rows in set (0.00 sec) 以id分组,把name字段的值打印在一行,逗号分隔(默认) mysql> select id,group_concat(name) from aa group by id; +------+--------------------+ | id| group_concat(name) | +------+--------------------+ |1 | 10,20,20| |2 | 20 | |3 | 200,500| +------+--------------------+ 3 rows in set (0.00 sec) 以id分组,把name字段的值打印在一行,分号分隔 mysql> select id,group_concat(name separator ';') from aa group by id; +------+----------------------------------+ | id| group_concat(name separator ';') | +------+----------------------------------+ |1 | 10;20;20 | |2 | 20| |3 | 200;500 | +------+----------------------------------+ 3 rows in set (0.00 sec) 以id分组,把去冗余的name字段的值打印在一行, 逗号分隔 mysql> select id,group_concat(distinct name) from aa group by id; +------+-----------------------------+ | id| group_concat(distinct name) | +------+-----------------------------+ |1 | 10,20| |2 | 20 | |3 | 200,500 | +------+-----------------------------+ 3 rows in set (0.00 sec) 以id分组,把name字段的值打印在一行,逗号分隔,以name排倒序 mysql> select id,group_concat(name order by name desc) from aa group by id; +------+---------------------------------------+ | id| group_concat(name order by name desc) | +------+---------------------------------------+ |1 | 20,20,10 | |2 | 20| |3 | 500,200| +------+---------------------------------------+ 3 rows in set (0.00 sec) repeat()函数 用来复制字符串,如下'ab'表示要复制的字符串,2表示复制的份数 mysql> select repeat('ab',2); +----------------+ | repeat('ab',2) | +----------------+ | abab | +----------------+ 1 row in set (0.00 sec) 又如 mysql> select repeat('a',2); +---------------+ | repeat('a',2) | +---------------+ | aa | +---------------+ 1 row in set (0.00 sec) mysql向表中某字段后追加一段字符串: update table_name set field=CONCAT(field,'',str) mysql 向表中某字段前加字符串 update table_name set field=CONCAT('str',field) 这个函数对你也许会有很大帮助哦!!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值