1.字符函数
1.1 CONCAT 拼接字符
1 | select concat("hello","world"); |
1.2 LENGTH 获取字节长度
1 | select length("hello world"); |
1 | select length("尚文杰"); |
1.3 CHAR_LENGTH 获取字符长度
1 | select char_length("尚文杰"); |
1.4 SUBSTRING 截取字符串
1 | select substr("尚文杰",1,2); |
1 | select substr("尚文杰",2); |
1.5 INSTR 字符第一次出现的位置
1 | select instr("尚文杰","尚"); |
1.6 TRIM 去前后字符(默认去空格)
1 | select trim(" hello world "); |
1 | select trim("x" from "xxhello worldxx"); |
1.7 LPAD/RPAD 左填充/右填充
1 | select lpad("hello",10,"x"); |
1 | select rpad("hello",10,"x"); |
1.8 UPPER/LOWER 变大写/变小写
1 | select upper("hello"); |
1 | select lower("HELLO"); |
1.9 STRCMP 比较
1 | select strcmp("aa","bb"); |
1.9 LEFT/RIGHT 左截/右截
1 | select left("ab",1); |
1 | select right("ab",1); |
2.数学函数
2.1 ABS 绝对值
1 | select abs(-1); |
2.2 CEIL 向上取整 返回>=该整数的最小整数
1 | select ceil(1.09); |
2.3 FLOOR 向下取整 返回<=该整数的最大整数
1 | select floor(1.09); |
2.4 ROUND 四舍五入
1 | select round(1.09); |
1 | select round(1.8989,2); |
2.5 TRUNCATE 截断
1 | select truncate(1.8989,1); |
2.6 MOD 取余
1 | select mod(10,2); |
3.日期函数
3.1 NOW
1 | select now(); |
3.2 CURDATE 只取日期
1 | select curdate(); |
3.3 CURTIME 只取时间
1 | select curtime(); |
3.4 DATEDIFF 取时间差
1 | select datediff('2020-04-08','2020-05-09'); |
3.5 DATE_FORMAT 格式化日期
1 | select date_format(now(),'%Y年%m月%d日 %H时%m分%s秒'); |
3.6 STR_TO_DATE 按指定格式解析字符串为日期
1 | select str_to_date('05/04 2020','%m/%d %Y'); |
4.流程控制函数
4.1 IF
1 | select if(100>1,'yes','no'); |
4.2 CASE
1 | select case when 100>102 then 'yes' when 100>101 then "100>101" else "no" end; |