常见函数
-
格式:select 函数名(实参) 【from 表】;
-
分类:
-
单行函数:
-
字符函数:
a. length(“字符zifu”);//获取字节个数,汉字UTF8三字节,GBK2字节,英文都是1字节
b. concat(“str1”,“str2”);//拼接字符
c. upper(""), lower("");//大小写转换
d. substr(“str”,2,3);//截取从2往后3个长度字符,基1索引
e. instr(“str”,“substr”);//返回子串初始索引
f. trim(“aa” FROM “aaazaaazaa”);//总头尾删除子串,得到azaaaz
g. lpad(“str1”,len,“str2”),rpad(“str1”,len,“str2”);//str1左右填充str2以达到len长度的字符串
-
数学函数:
a. round(-1.25),ceil(-1.25),floor(-1.25);//四舍五入-1,向上取整-1,向下取整-2
b. truncate(2.23,1);//截断函数,小数后保留几位,2.2
c. mod(a,b);//取模
-
日期函数:
a. now();//返回当前时间
b. curdate();//当前日期
c. year(now()), year(“1996-08-12”), month(), monthname(), day(), hour();//返回具体时间
d. str_to_date(“1995-02-01”,"%y-%m-%d");//按固定格式解析字符串到时间
-
流程控制函数:
a. if(cond, stat1,stat2);//条件中真执行stat1,否则stat2
-
其他函数:
a. version();//数据库版本
b. database();//当前数据库
c. user();//当前用户
-
-
统计函数:
- 求和函数:sum(),sum(distinct id)
- 求平均:avg()
- 最大值:max()
- 最小值:min()
- 计算个数:count()
- 分组查询:select Max(salary) from table_name group by job_id;//以某字段分组
-