一、单表查询准备

  1. create table emp(
  2. id int primary key auto_increment,
  3. name varchar(20) not null,
  4. sex enum('male','female') not null default 'male', #大部分是男的
  5. age int(3) unsigned not null default 28,
  6. hire_date date not null,
  7. post varchar(50),
  8. post_comment varchar(100),
  9. salary double(15,2),
  10. office int, #一个部门一个屋子
  11. depart_id int
  12. );
  13. #插入记录
  14. #三个部门:教学,销售,运营
  15. insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
  16. ('YLY','male',18,'20210101','CN帅giegie代言人',6.33,401,1), #以下是教学部
  17. ('tom','male',78,'20150302','teacher',1000000.31,401,1),
  18. ('kevin','male',81,'20130305','teacher',8300,401,1),
  19. ('tony','male',73,'20140701','teacher',3500,401,1),
  20. ('owen','male',28,'20121101','teacher',2100,401,1),
  21. ('jack','female',18,'20110211','teacher',9000,401,1),
  22. ('jenny','male',18,'19000301','teacher',30000,401,1),
  23. ('sank','male',48,'20101111','teacher',10000,401,1),
  24. ('哈哈','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门
  25. ('呵呵','female',38,'20101101','sale',2000.35,402,2),
  26. ('西西','female',18,'20110312','sale',1000.37,402,2),
  27. ('乐乐','female',18,'20160513','sale',3000.29,402,2),
  28. ('拉拉','female',28,'20170127','sale',4000.33,402,2),
  29. ('僧龙','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门
  30. ('程咬金','male',18,'19970312','operation',20000,403,3),
  31. ('程咬银','female',18,'20130311','operation',19000,403,3),
  32. ('程咬铜','male',18,'20150411','operation',18000,403,3),
  33. ('程咬铁','female',18,'20140512','operation',17000,403,3);

二、查询关键字

  1. 1select
  2. 2from
  3. 3where
  4. 4group by
  5. 聚合函数
  6. having
  7. 3distinct
  8. 4order by

1、select命令

  1. select
  2. 控制查询表中的哪些字段对应的数据

2、from

  1. from
  2. 控制查询的表

3、where (分组前筛选)

  1. # where其实就是对数据进行筛选
  2. 格式:
  3. where [字段] in ();
  4. where [字段] not in ();
  5. where [字段] between num_a and num_b;
  6. 条件:有orand
  7. where [字段] like "% _";
  8. where [字段] >、<、= num_c;
  9. where [字段] is null;
  10. where [字段] char_length(num_d);
  11. 案例1.查询id大于等于3小于等于6的数据
  12. select id,name from emp where id >= 3 and id <= 6;
  13. select * from emp where id between 3 and 6;
  14. 案例2.查询薪资是20000或者18000或者17000的数据
  15. select * from emp where salary = 20000 or salary = 18000 or salary = 17000;
  16. select * from emp where salary in (20000,18000,17000); # 简写
  17. """
  18. 模糊查询
  19. 关键字 like
  20. 关键符号
  21. %:匹配任意个数的任意字符
  22. _:匹配单个个数的任意字符
  23. """
  24. 案例3.查询员工姓名中包含o字母的员工姓名和薪资
  25. select name,salary from emp where name like '%o%';
  26. 案例4.查询员工姓名为四个字符组成的员工姓名和薪资
  27. select name,salary from emp where name like '____';
  28. select name,salary from emp where char_length(name) = 4;
  29. 案例5.查询id小于3或者大于6的数据
  30. select * from emp where id not between 3 and 6;
  31. 案例6.查询薪资不在200001800017000范围的数据
  32. select * from emp where salary not in (20000,18000,17000);
  33. 案例7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
  34. select name,post from emp where post_comment = NULL; # 查询为空!
  35. select name,post from emp where post_comment is NULL;
  36. select name,post from emp where post_comment is not NULL;

4、group by

  1. 分组
  2. 按照某个指定的条件将单个单个的数据分为一个个整体
  3. eg:
  4. 咱班按照座位横向分组
  5. 咱班按照年龄分组
  6. 咱班按照省份分组
  7. 应用场景
  8. eg:
  9. 求每个部门的平均薪资
  10. 求每个国家的人均GDP
  11. 求男女平均薪资
  12. 格式:
  13. select [分组的信息] from [表名] [where筛选] group by [分组字段] [having过滤];
  14. """
  15. 分组之后不再以单个个体为研究对象 也无法直接再获取单个个体的数据
  16. 研究对象应该是分组的整体
  17. 分组之后默认只能直接获取到分组的依据 其他字段数据无法直接获取
  18. 如果需要实现上述要求 还是修改sql_mode
  19. set global sql_mode='only_full_group_by';
  20. """
  21. 案例1.获取每个部门的最大薪资
  22. select post,max(salary) from emp group by post;
  23. 案例2.统计每个部门的人数
  24. select post,count(id) from emp group by post;
  25. 案例3.获取每个部门的员工姓名
  26. select post,group_concat(name,'|',salary) from emp group by post;
  27. # group_concat用于分组之后获取分组以外的字段数据并支持拼接
  28. # concat用于分组之前的拼接操作
  29. select id,concat(name,'|',salary) as '嘿嘿' from emp;
  30. # concat_ws当多个字段连接符相同的情况下推荐使用
  31. select id,concat_ws('|',name,sex,salary,age) from emp;
  32. """
  33. 在查看结果的时候可以给字段起别名
  34. select post as '部门',max(salary) as '最高薪资' from emp group by post;
  35. as可以省略但是为了语义更加明确建议不要省略
  36. """

4.1 聚合函数

  1. max()
  2. min()
  3. sum()
  4. count()
  5. avg()
  6. # 上述聚合函数都是在分组之后使用 用于操作整体数据

4.2 having (分组后过滤)

  1. wherehaving都是用来筛选数据的
  2. 但是where用于分组之前的筛选、having用于分组之后的筛选
  3. 为了人为的区分开 我们将where用筛选来形容 having用过滤来形容
  4. # 统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
  5. select post,avg(salary) from emp where age > 30 group by post having avg(salary)>10000;
  6. """
  7. 将一个复杂的查询题拆分成多个简单的小题
  8. """

3、distinct去重

  1. """
  2. 去重的前提示是存在一模一样的数据
  3. 如果存在主键肯定无法去重
  4. """
  5. # 对有重复的展示数据进行去重操作 一定要是重复的数据
  6. select distinct id,age from emp;
  7. select distinct post from emp;

4、order by排序

  1. order by默认是升序 默认的关键字是asc
  2. select * from emp order by salary asc;
  3. 也可以改为降序 desc
  4. select * from emp order by salary desc;
  5. # order by排序支持多个字段组合(第一个不行 就往后继续排)
  6. select * from emp order by age,salary;
  7. select * from emp order by age asc,salary desc;

三、单表查询总结

1、写单表查询语句顺序

  1. 当查询比较复杂的时候,一步步写,先where筛选,后having过滤,最后再排序,去重。

2、关键词总结

  1. 单表查询的格式:
  2. SELECT [查询字段] FROM [表名] [where筛选语句] [显示规则] [having过滤条件]
  3. ----------------------------------------------------
  4. where
  5. like "% _"
  6. between
  7. in / not in
  8. and / or
  9. is / is not
  10. char_length(字段)=num
  11. group by
  12. 修改sql_mode
  13. set global sql_mode='only_full_group_by';
  14. group_concat("") #将多个字段值进行拼接
  15. distinct
  16. select distinct id,age from emp;
  17. order by
  18. select * from emp order by age,salary;
  19. ----------------------------------------------------
  20. 函数方法:
  21. 1
  22. concat(str1,str2,...) 将多个字段进行拼接
  23. 用法:
  24. select id,concat(name,'|',salary) from emp;
  25. concat_ws(separatorstr1,str2,...) 用分隔符将多个字段进行拼接
  26. 用法:
  27. select id,concat_ws('|',name,salary,age,post) from emp;
  28. group_concat(str1,str2,..) 分组之后的拼接
  29. 用法:
  30. select group_concat("|",name,"-",age,"-",post,"|") from emp group by post;
  31. 2
  32. max(str)
  33. 3
  34. min(str)
  35. 4
  36. sum(str1)
  37. 5
  38. count(id) # 计数有多少个不重复的数据
  39. 6
  40. avg(str1)

四、上述案例集合

  1. 1.查询id大于等于3小于等于6的数据
  2. 2.查询薪资是20000或者18000或者17000的数据
  3. 3.查询员工姓名中包含o字母的员工姓名和薪资
  4. 4.查询员工姓名为四个字符组成的员工姓名和薪资
  5. 5.查询id小于3或者大于6的数据
  6. 6.查询薪资不在200001800017000范围的数据
  7. 7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
  8. 1.获取每个部门的最大薪资
  9. 2.统计每个部门的人数
  10. 3.获取每个部门的员工姓名
  11. 1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门

五、测验

  1. 1. 查询岗位名以及岗位包含的所有员工名字
  2. 2. 查询岗位名以及各岗位内包含的员工个数
  3. 3. 查询公司内男员工和女员工的个数
  4. 4. 查询岗位名以及各岗位的平均薪资
  5. 5. 查询岗位名以及各岗位的最高薪资
  6. 6. 查询岗位名以及各岗位的最低薪资
  7. 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资

答案:

  1. 1select post as "岗位",group_concat(name) as "姓名" from emp group by post;
  2. 2select post as "岗位",count(id) as "员工个数" from emp group by post;
  3. 3select post as "岗位",count(sex="male") as "男员工个数",count(sex="female") as "女员工个数" from emp group by post;
  4. 4select post as "岗位",avg(salary) as "平均薪资" from emp group by post;
  5. 5select post as "岗位",max(salary) as "最高薪资" from emp group by post;
  6. 6select post as "岗位",min(salary) as "最低薪资" from emp group by post;
  7. 7select sex,avg(salary) from emp group by sex;