mysql

select version();
-- 8.0.18


##################################MySQL 字符串函数##################################
-- 返回字符串 s 的字符数
select CHAR_LENGTH('abcdef') as `length`; #6
select CHAR_LENGTH('哈哈') as `length`; #2
select CHARACTER_LENGTH('abcdef') as `length`; #6
select CHARACTER_LENGTH('哈哈') as `length`; #2

-- CONCAT(s1,s2...sn)
-- 字符串 s1,s2 等多个字符串合并为一个字符串
select concat('one', '-', 'two', '-', 'three') as `合并字符串`; #one-two-three

-- CONCAT_WS(x, s1,s2...sn)
-- 同 CONCAT(s1,s2,...) 函数,但是每个字符串之间要加上 x,x 可以是分隔符
select concat_ws('-', 'one', 'two', 'three') as `合并字符串`; #one-two-three

-- LOCATE(s1,s)
-- 从字符串 s 中获取 s1 的开始位置【特别注意位置是从1开始的】
select locate('st', 'verygondstring') as `开始位置`; #9

-- 将字符串 s 的所有字母变成大或小写字母
select lcase('abCDeF') as `全变小写`; #abcdef
select ucase('abCDeF') as `全变大写`; #ABCDEF
select lower('abCDeF') as `全变小写`; #abcdef
select upper('abCDeF') as `全变大写`; #ABCDEF

-- LEFT(s,n)
-- 返回字符串 s 的前 n 个字符
select left('abcdef', 2) as `结果`; #ab
select right('阖家安康很快就暗访', 2) as `结果`; #暗访

-- LTRIM(s)
-- 去掉字符串 s 开始处的空格
select ltrim(' sdhja ') as `去掉开头的空格`; #[sdhja ]
select rtrim(' sdhja ') as `去掉结尾的空格`; #[ sdhja]
select trim(' sdhja ') as `去掉首尾空格`; #[sdhja]

-- REPLACE(s,s1,s2)
-- 将字符串 s2 替代字符串 s 中的字符串 s1
select replace('abcdef', 'cd', '1234') as `替换指定字符`; #ab1234ef

-- SUBSTR(s, start, length)
-- 从字符串 s 的 start 位置截取长度为 length 的子字符串
select substr('abcdef', 2, 2) as `截取`; #bc
select substr('abcdef', 2) as `从第2个开始截取之后的全部`; #bcdef
select substring('哈师大的阿萨德', 2, 2) as `截取`; #师大
select substring('哈师大的阿萨德', 2) as `从第2个开始截取之后的全部`; #师大的阿萨德

-- FORMAT(x,n)
-- 函数可以将数字 x 进行格式化 "#,###.##", 将 x 保留到小数点后 n 位,最后一位四舍五入。
select format('23456.6789', 2) as `value`; #23,456.68
select format(23456.6789, 2) as `value`; #23,456.68

 

##################################MySQL 数字函数##################################
-- 向上取整
select ceil(1.5) as `result`; #2
select ceiling(1.3) as `result`; #2

-- 向下取整
select floor(1.6) as `result`; #1

-- 四舍五入
select round(1.4) as `result`; #1
select round(1.5) as `result`; #2

-- 集合函数
-- avg(`expression`)
-- count(`expression`)
-- max(`expression`)
-- min(`expression`)
-- sum(`expression`)

-- 返回 0 到 1 的随机数 
select rand();


##################################MySQL 日期函数##################################
select now() as `当前日期和时间`; #yyyy-MM-dd HH:mm:ss

select sysdate() as `当前日期和时间`; #yyyy-MM-dd HH:mm:ss

select curdate() as `当前日期`; #yyyy-MM-dd

select curtime() as `当前时间`; #HH:mm:ss

select adddate(now(), 1) as `加n天`;

select addtime(now(), 1000) as `加n秒`;

select date('2020/10/10 12:12:12') as `取日期部分`; #yyyy-MM-dd

select time('2020/10/10 12:12:12') as `取时间部分`; #HH:mm:ss

select datediff('2020-10-10', '2019-10-10') as `相隔天数`;

select dayname(now()) as `星期几`; #返回日期 d 是星期几,如 Monday,Tuesday

select dayofweek(now()) as `星期几`; #日期 d 今天是星期几,1 星期日,2 星期一,以此类推

select dayofyear(now()) as `本年第几天`;

select last_day(now()) as `本月最后一天`; #yyyy-MM-dd

select quarter(now()) as `第几个季节`; #1 ~ 4

select year(now()) as `年值`;

select month(now()) as `月值`; #1 ~ 12

select day(now()) as `日值`;

select hour(now()) as `时值`;

select minute(now()) as `分钟值`;

select second(now()) as `秒值`;

select sec_to_time(1234) as `以秒为单位的时间s转时分秒格式`; #HH:mm:ss

select time_to_sec('20:20:20') as `将时间 t 转换为秒`;

select timediff('2020-10-10 12:12:12', '2020-10-10 13:12:12') as `计算时间差值`; #[-]HH:mm:ss


##################################MySQL 高级函数##################################

set @param = 4;
select 
	case @param 
		when 1 then 'one' 
		when 2 then 'two' 
		else 'six' 
	end as result1,
	case
		when @param = 1 then 'one'
		when @param = 2 then 'two'
		else 'five'
		end as result2;
		
-- CAST(x AS type)
-- 转换数据类型
select cast('2017-09-09' as date) as result;

-- 返回唯一的连接 ID
select connection_id();

-- IF(expr,v1,v2)
-- 如果表达式 expr 成立,返回结果 v1;否则,返回结果 v2。
select if(1=2, 1, 2) as result; 

-- IFNULL(v1,v2)
-- 如果 v1 的值不为 NULL,则返回 v1,否则返回 v2。
select ifnull(null, 1) as result;

-- ISNULL(expression)
-- 判断表达式是否为 NULL, 为null返回1,否则返回0
select isnull('') as result;

-- 	返回最近生成的 AUTO_INCREMENT 值
select last_insert_id();

-- NULLIF(expr1, expr2)
-- 比较两个字符串,如果字符串 expr1 与 expr2 相等 返回 NULL,否则返回 expr1
select nullif(25, 25) as result;

  

原文地址:https://www.cnblogs.com/gygtech/p/13794130.html