文章目录
mysql 函数
1、单行函数
1.1 字符函数
字符函数:length(),concat(),upper(),lower(),substr(),substring(),instr(),trim(),lpad(),rpad(),replace()
》length(str):返回字符串的字节数
》substr(str,pos):从指定位置开始截取字符串
》substr(str,start,length):从指定位置开始截取指定长度(字符长度)的字符
》instr(str,substr):返回子串第一次出现的索引,如果找不到返回 0
》trim():去除字符串两端的空格 或 去掉字符串两端指定的字符
select trim(’ tom ‘) as name; # tom
select trim(‘a’ from ‘aaaa张aaa三丰aaaaa’) as name; # 张aaa三丰
》lpad(str,length,char):在字符串左边填充指定的字符,字符串总长度为 length。
select lpad(‘tom’,10,’*’) as name; # *******tom
select lpad(‘tom’,10,‘AB’) as name; # ABABABAtom
》replace(str,old,new):将原字符串中所有的指定的字符串替换成新的字符串
select replace(‘hello tom,kill tom’,‘tom’,‘cat’) as name; # hello cat,kill cat
1.2 数学函数
数学函数:round(),ceil(),floor(),truncate(),mod()
》round(X):四舍五入,对绝对值四舍五入后再加上符号(+ or -)
select round(12.357) as val; # 12
select round(12.53) as val; # 13
select round(-12.357) as val; # -12
select round(-12.53) as val; # -13
》round(X,D):四舍五入,保留指定小数位数
select round(12.357,2) as val; # 12.36
》ceil(X):向上取整,返回大于等于该参数的最小整数值
select ceil(12.357) as val; # 13
select ceil(-12.357) as val; # -12
》floor(X):向下取整
select floor(12.357) as val; # 12
select floor(-12.357) as val; # -13
》truncate(X,D):截断,小数点后保留指定位数
select truncate(12.357,1) as val; # 12.3
》mod(X,D):取余
select mod(12,5) as val; # 2
select mod(-12,5) as val; # -2
select mod(12,-5) as val; # 2
select mod(-12,-5) as val; # -2
【注意】余数是正还是负?如何确定?
死记法:余数的正负号与 X 的正负号相同
公式法:余数=X-X/D*D
1.3 日期函数
日期函数:now(),curdate()/current_date(),curtime()/current_time(),year(),month(),day(),hour(),minute(),second(),str_to_date(),date_format()
》now():返回当前系统日期 + 时间
select now(); # 2016-08-11 17:27:45
》curdate()/current_date():返回当前系统日期
select curdate(); # 2016-08-11
select current_date(); # 2016-08-11
》curtime()/current_time():返回当前系统时间
select curtime(); # 17:31:02
select current_time(); # 17:31:02
》year():获取当前系统年份
select year(now()) as year; # 2016
select year(‘2016-08-11’) as year; # 2016
》month():获取当前系统日
select month(now()) as month; # 8
》day():获取当前系统日
select day(now()) as day; # 11
》str_to_date(str,format):用指定格式去解析日期格式的字符串,返回日期
select str_to_date(‘8-11-2016’,’%m-%d-%Y’) as date; # 2016-08-11
》date_format(date,format):将日期转换成指定格式的字符串
select date_format(now(),’%Y年%m月%d日 %H:%i:%S’) as output; # 2018年08月11日 17:49:57
1.4 其他函数
其他函数:version(),database(),user()
》version():版本号
select version(); # 5.7.23
》database():当前数据库
select database(); # mytestdb
》user():当前用户
select user(); # [email protected]
1.5 流程控制函数
流程控制函数:if()
》if(exp1,exp2,exp3):如果 exp1 表达式的结果为 true,则整个表达式返回 exp2 表达式的值,否则返回 exp3 表达式的值
select if(10>5,‘大’,‘小’); # 大
别外,case 也可以控制流程
情况一:case 后面有东西】
case 要判断的字段或表达式
when 常量1 then 要显示的值1 或 语句1;
when 常量2 then 要显示的值2 或 语句2;
…
else 要显示的值n 或 语句n;
end
SELECT
dept,salary,
CASE dept
WHEN 20 THEN 2 * salary
WHEN 30 THEN 3 * salary
ELSE salary
END AS new_salary
FROM
(
select 10 dept,1000 salary from dual union
select 20,1500 from dual union
select 30,1000 from dual union
select 40,1000 from dual
) t;
+------+--------+------------+
| dept | salary | new_salary |
+------+--------+------------+
| 10 | 1000 | 1000 |
| 20 | 1500 | 3000 |
| 30 | 1000 | 3000 |
| 40 | 1000 | 1000 |
+------+--------+------------+
情况二:case 后面没有东西】
case
when 条件1 then 要显示的值1 或 语句1;
when 条件2 then 要显示的值2 或 语句2;
…
else 要显示的值n 或 语句n;
end
SELECT
dept,salary,
CASE
WHEN salary>3000 THEN 'A'
WHEN salary>2000 THEN 'B'
WHEN salary>1000 THEN 'C'
ELSE 'D'
END AS salary_level
FROM
(
select 10 dept,1000 salary from dual union
select 20,2300 from dual union
select 30,3700 from dual union
select 40,4100 from dual
) t;
+------+--------+--------------+
| dept | salary | salary_level |
+------+--------+--------------+
| 10 | 1000 | D |
| 20 | 2300 | B |
| 30 | 3700 | A |
| 40 | 4100 | A |
+------+--------+--------------+
2、分组函数
功能:用作统计使用,又称聚合函数或统计函数或组函数。
2.1 分组函数分类
分类:sum()、avg()、max()、min()、count()
特点:
1、参数类型
sum()、avg() 一般用于处理数值型。
max()、min()、count() 可以处理任何类型。
2、是否忽略 null 值
以上分组函数都忽略 null 值,null 值不参与计算。
3、可以和 distinct 搭配
以上分组函数都可以和 distinct 搭配使用
select sum(DISTINCT salary) sum from (
select 10 dept,1000 salary from dual union
select 20,2000 from dual union
select 30,3000 from dual union
select 40,4000 from dual union
select 50,3000 from dual
) t;
+-------+
| sum |
+-------+
| 10000 |
+-------+
2.2 count() 函数详细介绍
count(expr):返回 expr 非 null 值的计数。
count():返回所检索行数的计数,不管它们是否包含 NULL 值。
count(distinct expr,[expr…]):返回去重后的非 null 值的计数。
count(1):表示给表额外加一列,值都为 1,然后统计该列的行数。
MYISAM 存储引擎下,count()的效率最高。
INNODB 存储引擎下,count(*) 和 count(1) 的效率差不多,它们比 count(字段)效率略高。
2.3 和分组函数一同查询的字段有限制
和分组函数一同查询的字段只能是 group by 后面的字段。
select sum(salary),name from user group by name;