1.常用函数
字符串处理函数
- length(str)
统计字符长度 - char_length(str)
统计以(单个字符为单位)的字符长度 - ucase/upper(str)
小写变大写 - lcase/lower(str)
大写变小写 - substr(s,start,end)
从s截start到end的字符串 - instr(str,"str1")
str1在str的位置是? trim(str)
去掉左右边的空格
数字函数
- abs()
绝对值 - pi()
圆周率 - mod(x,y)
x%d? - ceil/ceiling(x)
返回不小于x的最小整数 (x 是小数)
- floor(x)
返回不大于x的最大整数 (x 是有小数的数字)
日期函数
- curtime()
当前时间 - curdate()
当前日期 - now()
当前日期+时间 - time/date/year/month/day/hour/minute/second(now())
当前日期+时间中的时间/日期/年/月/日/时/钟/秒 - dayofmonth/dayofyear/monthname/dayname/quarter/week/weekday(curdate())
统计当前日期的{当月的第几天}/{当前的第几天}/{几月}/{星期几}/{哪个季度}/{一年中的第几周}/{一周中的第几天}
聚集函数
用于统计数值类型的表头数据,注意聚合函数!!!(待补充)
- sum(头)
求和 - avg(头)
求平均值 - min/max(tou)
求最小值/最大值 - count(头)
统计表头值个数 - if函数
if(条件,v1,v2) //满足走v1,否则v2 ifnull(v1,v2) //不是null走v1,否则v2
- case函数
select dept_id, dept_name, case dept_name when '运维部' then '技术部门' when '开发部' then '技术部门' when '测试部' then '技术部门' else '非技术部门' end as 别名
2.查询结果处理
- 分组
select count(name),dep_id from people group by dep_id; //查询每个部门id中有多少name,grouby by 后跟<不重复的表头>,重复的需要圈起来数羊
- 排序
select name,salary from people order by salary asc或者desc; //根据salart的大小进行排序 升序或者降序
- 过滤
select count(name),dep_id from people group by dep_id having where salary between 3000 and 5000; //在salary在3000-5000范围内,根据dep_id对name分组 //having 用于带有group by 的语句,where无法使用
- 分页
select count(name),dep_id from people group by dep_id limit 5; //输出前5行数据 select count(name),dep_id from people group by dep_id limit n,m; //输出第n行到m行数据 select count(name),dep_id from people group by dep_id limit 5 offset(2-1)*5; //输出第二页页大小为5的数据 //offset 表示从第几页开始
3.表内容管理
- 插入
//单行插入 insert into people(name,dep_t,salary)values("liwu",1001,5000); //多行插入 insert into tarena.user values ("liwu",1001,5000); ("liwu",1001,5000); //使用set赋值 insert into people set name="yaya",dep_id=1003,salary=6900;
- 删除
delete from people where name="ysys";
- 修改
//加条件修改 update people set salary=9999 where name="yaya"; //不加条件就批量修改了 update people set salary=8888;