MySQL——表查询

单表查询

前期准备

1create table emp( 2 id int not null unique 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#三个部门:教学,销售,运营 16insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values 17('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部 18('egon','male',78,'20150302','teacher',1000000.31,401,1), 19('kevin','male',81,'20130305','teacher',8300,401,1), 20('tank','male',73,'20140701','teacher',3500,401,1), 21('owen','male',28,'20121101','teacher',2100,401,1), 22('jerry','female',18,'20110211','teacher',9000,401,1), 23('nick','male',18,'19000301','teacher',30000,401,1), 24('sean','male',48,'20101111','teacher',10000,401,1), 25 26('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门 27('丫丫','female',38,'20101101','sale',2000.35,402,2), 28('丁丁','female',18,'20110312','sale',1000.37,402,2), 29('星星','female',18,'20160513','sale',3000.29,402,2), 30('格格','female',28,'20170127','sale',4000.33,402,2), 31 32('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门 33('程咬金','male',18,'19970312','operation',20000,403,3), 34('程咬银','female',18,'20130311','operation',19000,403,3), 35('程咬铜','male',18,'20150411','operation',18000,403,3), 36('程咬铁','female',18,'20140512','operation',17000,403,3) 37; 38 39#ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk

前期准备

1 执行顺序

1# 初识查询语句 2select id,name from emp where id >= 3 and id <= 6; 3# 先后顺序 4from 5where 6select

2 where 约束条件

1# 1.查询id大于等于3小于等于6的数据 2select id,name from emp where id >= 3 and id <= 6; 3select * from emp where id between 3 and 6; 4 5# 2.查询薪资是20000或者18000或者17000的数据 6select * from emp where salary = 20000 or salary = 18000 or salary = 17000; 7select * from emp where salary in (20000,18000,17000); # 简写 8 9# 3.查询员工姓名中包含o字母的员工姓名和薪资 10# 在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句 11""" 12先是查哪张表 from emp 13再是根据什么条件去查 where name like ‘%o%14再是对查询出来的数据筛选展示部分 select name,salary 15""" 16select name,salary from emp where name like '%o%'; 17 18# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资 19select name,salary from emp where name like '____'; 20select name,salary from emp where char_length(name) = 4; 21 22# 5.查询id小于3或者大于6的数据 23select * from emp where id not between 3 and 6; 24 25# 6.查询薪资不在200001800017000范围的数据 26select * from emp where salary not in (20000,18000,17000); 27 28# 7.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is 29select name,post from emp where post_comment = NULL; # 查询为空! 30select name,post from emp where post_comment is NULL; 31select name,post from emp where post_comment is not NULL;

where 约束条件

3 group by :分组

1# 数据分组应用场景:每个部门的平均薪资,男女比例等 2 3# 1.按部门分组 4select * from emp group by post; # 分组后取出的是每个组的第一条数据 5select id,name,sex from emp group by post; # 验证 6""" 7设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据, 8不应该在去取组里面的单个元素的值,那样的话分组就没有意义了,因为不分组就是对单个元素信息的随意获取 9""" 10set global sql_mode="strict_trans_tables,only_full_group_by"; 11# 重新链接客户端 12select * from emp group by post; # 报错 13select id,name,sex from emp group by post; # 报错 14select post from emp group by post; # 获取部门信息 15# 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名 16 17 18# 2.获取每个部门的最高工资 19# 以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果) 20# 每个部门的最高工资 21select post,max(salary) from emp group by post; 22# 每个部门的最低工资 23select post,min(salary) from emp group by post; 24# 每个部门的平均工资 25select post,avg(salary) from emp group by post; 26# 每个部门的工资总和 27select post,sum(salary) from emp group by post; 28# 每个部门的人数 29select post,count(id) from emp group by post; 30 31# 3.查询分组之后的部门名称和每个部门下所有的学生姓名 32# group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用 33select post,group_concat(name) from emp group by post; 34 35select post,group_concat(name,"_SB") from emp group by post; 36 37select post,group_concat(name,": ",salary) from emp group by post; 38 39select post,group_concat(salary) from emp group by post;

group by

4  group_conct/concat/as

1# 3.查询分组之后的部门名称和每个部门下所有的学生姓名 2# group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用 3select post,group_concat(name) from emp group by post; 4 5select post,group_concat(name,"_SB") from emp group by post; 6 7select post,group_concat(name,": ",salary) from emp group by post; 8 9select post,group_concat(salary) from emp group by post; 10 11 12# 4.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用 13select name as 姓名,salary as 薪资 from emp; 14select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp; 15 16# 补充as语法 即可以给字段起别名也可以给表起 17select emp.id,emp.name from emp as t1; # 报错 因为表名已经被你改成了t1 18select t1.id,t1.name from emp as t1;

补充

练习题:

1# 刚开始查询表,一定要按照最基本的步骤,先确定是哪张表,再确定查这张表也没有限制条件,再确定是否需要分类,最后再确定需要什么字段对应的信息 2 31. 查询岗位名以及岗位包含的所有员工名字 42. 查询岗位名以及各岗位内包含的员工个数 53. 查询公司内男员工和女员工的个数 64. 查询岗位名以及各岗位的平均薪资 75. 查询岗位名以及各岗位的最高薪资 86. 查询岗位名以及各岗位的最低薪资 97. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资 10""" 11参考答案: 12select post,group_concat(name) from emp group by post; 13select post,count(id) from emp group by post; 14select sex,count(id) from emp group by sex; 15select post,avg(salary) from emp group by post; 16select post,max(salary) from emp group by post; 17select post,min(salary) from emp group by post; 18select sex,avg(salary) from emp group by sex; 19""" 20 21# 关键字where group by同时出现的情况下,group by必须在where之后 22# where先对整张表进行一次筛选,如何group by再对筛选过后的表进行分组 23# 如何验证where是在group by之前执行而不是之后 利用聚合函数 因为聚合函数只能在分组之后才能使用 24select id,name,age from emp where max(salary) > 3000; # 报错! 25 26select max(salary) from emp; 27# 正常运行,不分组意味着每一个人都是一组,等运行到max(salary)的时候已经经过where,group by操作了,只不过我们都没有写这些条件 28 29# 语法顺序 30select 31from 32where 33group by 34 35# 再识执行顺序 36from 37where 38group by 39select 40 41 428、统计各部门年龄在30岁以上的员工平均工资 43select post,avg(salary) from emp where age > 30 group by post; 44# 对where过滤出来的虚拟表进行一个分组 45 46# 还不明白可以分步执行查看结构 47select * from emp where age>30; 48# 基于上面的虚拟表进行分组 49select * from emp where age>=30 group by post;;

练习

5 having:筛选

having的语法格式与where一致,只不过having是在分组之后进行的过滤,即where虽然不能用聚合函数,但是having可以!

11、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门 2select post,avg(salary) from emp 3 where age >= 30 4 group by post 5 having avg(salary) > 10000; 6# 如果不信你可以将having取掉,查看结果,对比即可验证having用法! 7 8#强调:having必须在group by后面使用 9select * from emp having avg(salary) > 10000; # 报错

having

6 distinct : 去重

1# 对有重复的展示数据进行去重操作 2select distinct post from emp;

distinct

7 order by:排序

1select * from emp order by salary asc; #默认升序排 2select * from emp order by salary desc; #降序排 3 4select * from emp order by age desc; #降序排 5 6#先按照age降序排,在年轻相同的情况下再按照薪资升序排 7select * from emp order by age desc,salary asc; 8 9# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序 10select post,avg(salary) from emp 11 where age > 10 12 group by post 13 having avg(salary) > 1000 14 order by avg(salary) 15 ;

order by

8 limit:限制

1select * from emp order by salary asc; #默认升序排 2select * from emp order by salary desc; #降序排 3 4select * from emp order by age desc; #降序排 5 6#先按照age降序排,在年轻相同的情况下再按照薪资升序排 7select * from emp order by age desc,salary asc; 8 9# 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序 10select post,avg(salary) from emp 11 where age > 10 12 group by post 13 having avg(salary) > 1000 14 order by avg(salary) 15 ;

limit

9 正则

select * from emp where name regexp '^j.*(n|y)$';

正则

多表查询

前期准备:

1#建表 2create table dep( 3id int, 4name varchar(20) 5); 6 7create table emp( 8id int primary key auto_increment, 9name varchar(20), 10sex enum('male','female') not null default 'male', 11age int, 12dep_id int 13); 14 15#插入数据 16insert into dep values 17(200,'技术'), 18(201,'人力资源'), 19(202,'销售'), 20(203,'运营'); 21 22insert into emp(name,sex,age,dep_id) values 23('jason','male',18,200), 24('egon','female',48,201), 25('kevin','male',38,201), 26('nick','female',28,202), 27('owen','male',18,200), 28('jerry','female',18,204) 29; 30 31# 当初为什么我们要分表,就是为了方便管理,在硬盘上确实是多张表,但是到了内存中我们应该把他们再拼成一张表进行查询才合理

多表前期准备

1 多表查询

1select * from emp,dep; # 左表一条记录与右表所有记录都对应一遍>>>笛卡尔积 2 3# 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据 4 5# 查询员工及所在部门的信息 6select * from emp,dep where emp.dep_id = dep.id; 7# 查询部门为技术部的员工及部门信息 8select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术'; 9 10 11# 将两张表关联到一起的操作,有专门对应的方法 12# 1、内连接:只取两张表有对应关系的记录 13select * from emp inner join dep on emp.dep_id = dep.id; 14select * from emp inner join dep on emp.dep_id = dep.id 15 where dep.name = "技术"; 16 17# 2、左连接: 在内连接的基础上保留左表没有对应关系的记录 18select * from emp left join dep on emp.dep_id = dep.id; 19 20# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录 21select * from emp right join dep on emp.dep_id = dep.id; 22 23# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录 24select * from emp left join dep on emp.dep_id = dep.id 25union 26select * from emp right join dep on emp.dep_id = dep.id;

多表查询方式

2 子表查询:就是将一个查询语句的结果用括号括起来当作另外一个查询语句的条件去用

1# 1.查询部门是技术或者人力资源的员工信息 2""" 3先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息 4""" 5select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源"); 6 7# 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询 8select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1 9inner join 10(select post,max(hire_date) as max_date from emp group by post) as t2 11on t1.post = t2.post 12where t1.hire_date = t2.max_date 13; 14 15""" 16记住一个规律,表的查询结果可以作为其他表的查询条件,也可以通过其别名的方式把它作为一张虚拟表去跟其他表做关联查询 17""" 18 19select * from emp inner join dep on emp.dep_id = dep.id;

子表查询方式

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )