向表中插入一条数据
insert into 表名 (列1,列2) values(value1,value2);
更新记录
update 表名 set 列名=value where id=”1“;
删除记录
delete from 表名 where id=”1“;
查询所有记录
select * from 表名;
查询不重复的记录
select district 列1,列2,列3 from 表名;
条件查询:where 支持 = > < >= <= != 多个条件之间 是or 或 and
select * from 表名 where 列=value and 列2=value2;
排序和限制;order by 默认是升序排序
select * from 表名 order by 列名;
按两个列排序,列1升序 值一样,再列2 降序;
select * from 表名 order by 列1,列2 desc;
查询按列1排序之后的前三个
select * from 表名 order by 列1 limit 3;
查询按列1 排序后的第二个开始的 三个记录
select * from 表名 order by 列1 limit 1,3;
聚合:
select [field1,field2] fun_name
from 表名
[where where_contition]
[group by field1,field2
[with rollup]]
[having where_contition]
上述参数说明:fun_name 表示要做的聚合操作,也就是聚合函数,常用的有sum(*)count(*) max(*) min(*)等
group by 关键字表示要进行分类聚合的字段,比如安装班级分类统计学生的数量,班级对应的字段就应该放到group by 后面;
with rollup 是可选语法,表明是否 对匪类聚合后的结果再进行汇总;having 关键字表示对分类后的结果再进行过滤;
{havaing 和 where 的区别在于,having 是对聚合后的结果在进行条件过滤,where 是对聚合前的记录进行过滤}
统计总条数
select count(*) from 表名;
统计各个班级的人数
select 班级列名,count(*) from 表名 group by 班级列名;
统计各个班级的人数之后,再统计 统计之后的总数;with rollup
select 班级列名,count(*) from 表名 group by 班级列名 with rollup;
统计班级人数大于10的
select 班级列名,count(1) from 表名 group by 班级列名 having count(1)>10;