2月17,无声无息的就开学了,并不开心,,,,,
报了399的班,即将开始漫长的学习生涯,加油,毕业找个好工作。。。。
以下为查询内容:
-- 完整的查询指令:
-- select select选项 字段列表 from 数据源 where 条件 group by 分组 having 条件 order by 排序 limit 限制;
-- select 选项
-- all:默认的表示保存所有的结果
select all * from my_gbk; -- 等价于 select *
-- distinct:去重,去除重复的记录(所有的字段都相同),只保留一条
select distinct * from my_gbk;
-- 字段列表
-- 不同表中有同名字段,需要将同名的字段命名成不同名的
-- 格式:字段名 [as] 别名
-- 字段取别名
select distinct name as name1, name name2 from my_gbk;
-- from 数据源
-- 格式:from 表名;
-- 从多张表获取数据(字段数两两拼接,记录数两两相乘)
-- 格式:from 表1, 表2, ...
select * from my_int, my_gbk;
-- 动态数据
-- from 后面是一个从表中查询出来得到的二维结果表
-- 格式:from (select 字段列表 from 表) as 别名;
select * from (select int_1,int_8 from my_int) as int_my;
-- where 子句,获取数据时进行条件筛选
-- group by:根据指定的字段,将数据进行分组
-- 格式:group by 字段名;
-- 综合应用
show tables; -- 查看是否有student表
select * from my_student;
-- 插入数据
insert into my_student values(1314110503,"jack"),(1314110504, "lilei");
-- 插入新的字段(班级)
alter table my_student add class_id int;
-- 给班级字段插入数据
update my_student set class_ID = 1 where stu_ID in (1314110501,1314110502);
update my_student set class_ID = 2 where stu_ID in (1314110503,1314110504);
-- 分组 group by
-- group by 将数据按照指定的字段分组后,只会保留每组的第一条记录
select * from my_student group by class_ID; -- 根据班级ID进行分组
-- 一次加入多个字段
alter table my_student add stu_age tinyint unsigned,
add stu_hight tinyint unsigned;
-- 插入数据
update my_student set stu_age =18, stu_hight=175 where stu_ID=1314110501;
update my_student set stu_age =20, stu_hight=150 where stu_ID=1314110502;
update my_student set stu_age =15, stu_hight=140 where stu_ID=1314110503;
update my_student set stu_age =30, stu_hight=178 where stu_ID=1314110504;
-- 使用聚合函数(统计函数)
-- 统计每班人数,最大的年龄,最矮的,平均年龄
-- 将两个班作为一个整体进行统计
select class_ID, count(stu_age), max(stu_age), min(stu_hight), avg(stu_age) from my_student; -- 默认后面添有group by anything
-- 两个班分别进行统计
select class_ID, count(*), max(stu_age), min(stu_hight), avg(stu_age) from my_student group by class_ID;
-- group_concat():将分组中指定的字段进行合并(字符串的拼接)
select group_concat(stu_name), class_ID, count(*), max(stu_age), min(stu_hight), avg(stu_age) from my_student group by class_ID;
-- 结果:该组中的所有字段名连在一起显示
-- 多分组:对已经按照某个字段分组后的数据再分组
-- 格式:group by 字段1, 字段2; //先按照字段1进行排序,之后再将结果按照字段2进行排序
-- 枚举的方法插入字段
alter table my_student add gender enum("男","女","wifi");
-- 插入记录(多个记录用in )
update my_student set gender = 1 where stu_ID in (1314110502,1314110504);
update my_student set gender = 2 where stu_ID in (1314110501,1314110503);
-- 再插入3条数据
insert into my_student values (1314110505,"海波",1,40,170,1),(1314110506,"海清",1,40,150,2),(1314110507,"song",1,37,150,2);
-- 多分组:(选择?在?表 group by ?)
select class_ID, gender, group_concat(stu_name) ,count(gender) from my_student group by class_ID,gender;
-- 分组排序:按照分组字段进行排序,默认是升序
-- 格式:group by 字段 asc|desc, 字段 asc|desc ; //asc:升序, desc:降序
select class_ID, gender, group_concat(stu_name) ,count(gender) from my_student group by class_ID desc, gender desc;
-- 回溯统计:层层上报统计的过程
-- 格式:group by 字段 asc|desc with rollup;
-- 一班 --> 二班 --> 按照班级字段进行统计的两个班
select class_ID, count(*) from my_student group by class_ID with rollup;
update my_student set class_ID = 3 where stu_ID in (1314110507);
-- 多分组的回溯统计
select class_ID,gender, count(*) from my_student group by class_ID,gender with rollup;