一.库的操作
二.表操作
l 切换数据库 use 数据库名;
l
l 查看库内有哪些表 show tables;
l 删除表 drop table 表名;
l 查看表结构 desc 表名;
l 修改表的字段结构类型 alter table 表名 modify 字段名 字段类型; 5.修改表的字段名 alter table 表名 change 旧字段名 新字段名 字段类
l 6. 向表内增加新字段
l alter table 表名 add 字段名 数据类型 (默认添加到表的最后)
l 7.向表内第一个位置添加新字段
l alter table 表名 add 字段名 数据类型 first
l 8.向指定位置添加新字段
l alter table 表名 add 字段名 数据类型 after 字段名
l 9.修改表的名字
l alter table 表名 rename 新表名;
l 10.删除表字段
l alter table 表名 drop 字段名
三.类型
四.条件
l .比较运算符
l 等于: =
l 大于: >
l 大于等于: >=
l 小于: <
l 小于等于: <=
l 不等于: != 或 <>
l 2.逻辑运算符
l and
l or
l not
l 3..模糊查询
l like
l %表示任意多个任意字符
l _表示一个任意字符
l 4.范围查询
l in表示在一个非连续的范围内
l 查询编号是1或3或8的学生
l select * from students where id in(1,3,8);
l between ... and ...表示在一个连续的范围内
l :查询编号为3至8的学生
l not between ... and ...表示不在一个连续的范围内
l :查询 年龄不在在18到34之间的的信息
l 5..空判断
l 注意:null与‘‘是不同的
l 判空is null
l 例:查询没有填写身高的学生
l select * from students where height is null;
l 判非空is not null
l 例:查询填写了身高的学生
l select * from students where height is not null;
l 例:查询填写了身高的男生
l select * from students where height is not null and gender=1;
l 6.优先级
l 优先级由高到低的顺序为:小括号,not,比较运算符,逻辑运算符
l and比or先运算,如果同时出现并希望先算or,需要结合()使用
l 7.去重查询(重复记录只查询一次)
l 1、使用关键字distinct可以去除查询结果中的重复记录
l 2、语法格式:
l select distinct 字段名 from 表名;
l 8. 1.、显示前3条记录 select * from 表名 limit 3;
l 2.、使用关键字limit还可以查询结果的中间部分取值。
l 两个参数,参数1是开始读取的第一条记录的编号(在查询结果中,第一个结果的记录编号是0,而不是1);
l 参数2是要查询记录的个数。
l 例如:查询出第2条到第4条记录信息 select * from 表名 limit 1,3; 234
五.排序
l order by
l select * from 表名 order by 列1 asc|desc [,列2 asc|desc,...]
l order by 字段 默认是升序
l asc从小到大排列,即升序
l desc从大到小排序,即降序
六.聚合函数
总数:count
最大值:max
最小值:min
求和:sum
平均值:avg
四舍五入:round
计算男性的平均身高 保留2位小数
select round(avg(height), 2) from students where gender=1;
七.分组
group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组
group by可用于单个字段分组,也可用于多个字段分组
.group by 综合使用
1.group by + group_concat()
group_concat(字段名)可以作为一个输出字段来使用, 表示分组之后,根据分组结果,使用group_concat()来放置每一组的某字段的值的集合
select gender,group_concat(name) from students group by gender;
2.group by + having
having 条件表达式:用来分组查询后指定一些条件来输出查询结果
having作用和where一样,但having只能用于group by
select gender,count(*) from students group by gender having count(*)>2;
3.group by + with rollup
with rollup的作用是:在最后新增一行,来记录当前列里所有记录的总和
select gender,count(*) from students group by gender
原文:https://www.cnblogs.com/mxq-213/p/14888700.html