数据库之表的查询方式
一:where查询方式
(1)书写顺序:
例如:
select id,name from emp where id > 3 and id < 6;
(2)执行顺序:
(1)from :确定查询那个表
(2)where:筛选数据查找条件
(3)select:从筛选出来的数据条件中取出数据
(3)练习:
create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum('male','female') not null default 'male', #大部分是男的 age int(3) unsigned not null default 28, hire_date date not null, post varchar(50), post_comment varchar(100), salary double(15,2), ); #三个部门:教学,销售,运营 insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values ('jason','male',18,'20170301','张江第一帅形象代言',7300.33,401,1), #以下是教学部 ('egon','male',78,'20150302','teacher',1000000.31,401,1), ('kevin','male',81,'20130305','teacher',8300,401,1), ('tank','male',73,'20140701','teacher',3500,401,1), ('owen','male',28,'20121101','teacher',2100,401,1), ('jerry','female',18,'20110211','teacher',9000,401,1), ('nick','male',18,'19000301','teacher',30000,401,1), ('sean','male',48,'20101111','teacher',10000,401,1), ('歪歪','female',48,'20150311','sale',3000.13,402,2),#以下是销售部门 ('丫丫','female',38,'20101101','sale',2000.35,402,2), ('丁丁','female',18,'20110312','sale',1000.37,402,2), ('星星','female',18,'20160513','sale',3000.29,402,2), ('格格','female',28,'20170127','sale',4000.33,402,2), ('张野','male',28,'20160311','operation',10000.13,403,3), #以下是运营部门 ('程咬金','male',18,'19970312','operation',20000,403,3), ('程咬银','female',18,'20130311','operation',19000,403,3), ('程咬铜','male',18,'20150411','operation',18000,403,3), ('程咬铁','female',18,'20140512','operation',17000,403,3) ;
# 1.查询id大于等于3小于等于6的数据 select * from emp where id >= 3 and id <= 6; select name,salary from emp where id between 3 and 6; # 查询id(3-6)之间姓名 薪资 # 2.查询薪资是20000或者18000或者17000的数据 select * from emp where salary = 20000 or salary = 18000 or salary = 17000; select name,id from emp where salary in (20000,18000,17000); 查看薪资在(20000 180000 17000)姓名 ID # 3.查询员工姓名中包含o字母的员工姓名和薪资 select name,id from emp where name like '%o%'; # 模糊匹配 匹配出所有含有o字母 # 4.查询员工姓名是由四个字符组成的员工姓名与其薪资 select name,salary from emp where name like '____'; select name,salary,id from emp where char_length(name) = 4; # 5.查询id小于3或者大于6的数据 select * from emp where id > 3 or id < 6; select * from emp where id not between 3 and 6; # 6.查询薪资不在20000,18000,17000范围的数据 select * from emp where salary not in(20000,18000,17000); # 7.查询岗位描述为空的员工名与岗位名 select * from emp where post_comment = Null; # 不会报错 但是会匹配不到数据 select * from emp where post_comment is Null; # null 需要用is
二:分组
书写顺序:
例如:
select * from emp group by post;
执行顺序:
(1):from
(2):where
(3):group by
(4):select
例如:
select * from emp group by post; PS: (1)分组获取的以组为单位 获取的是组内的第一条数据 (2)在MySQL分组之后 只能获取分组后组内的字段信息 无法获取其余字段信息 (3)上述可以通过聚合函数 获取其余字段信息 (4)如果分组之后 依然可以获取到其余字段信息 说明没有设置严格模式 # 严格模式设置 set global sql_mode = "strict_trans_tables,only_full_group_by"; select * from emp group by post; # 报错 select name from emp group by post; # 报错 select post from emp group by post; # 查询成功
(2)聚合函数:
例如:
# 1.获取每个部门的最高工资 select post,max(salary) from emp group by post; # 每个部门的最低工资 select post,min(salary) from emp group by post; # 每个部门的平均工资 select post,avg(salary) from emp group by post; # 每个部门的工资总和 select post,sum(salary) from emp group by post; # 每个部门的人数 select post,count(id) from emp group by post; select post,count(salary) from emp group by post; select post,count(null) from emp group by post; # 无法统计 PS:在统计分组个数的时候 可以将非空字段当做参数传入count 完成计数
(3)concat/group_concat:
作用:
(1)group_concat:在分组之后用来进行数据的拼接
(2)concat:分组之前进行数据的拼接
例如:
# 3.查询分组之后的部门名称和每个部门下所有的姓名 select post,name from emp group by post; # 报错 select post,group_concat(name) from emp group by post; # 查询分组之后的部门名称和每个部门下所有的姓名 并将选出的数据名添加_SR select post,group_concat(name,'_SR') from emp group by post; # 查询分组之后的部门名称和每个部门下所有的姓名与薪水 并将选出的数据进行拼接 select post,group_concat(name,':',salary) from emp group by post; # 查询分组之后的部门名称和每个部门下所有的姓名与薪水 并将选出的数据进行拼接且通过as重命名表名 select concat('NAME',':',name) as 姓名,concat('SLA',':',salary) as 薪水 from emp where id between 3 and 6;
(4)having:
作用:对分组筛选的数据在进行筛选
例如:
1、统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门 select post from emp where age > 30 group by post having avg(salary) >10000; PS: (1):having只能用在分组后面 (2):having不能单独使用 select post from emp where age > 30 having avg(salary) >10000; # 报错 select post from emp having avg(salary) >10000; # 报错
(4)distinct:
作用:去除筛选字段中重复的数据
例如:
select distinct id,age from emp; # 无法筛选 因为此时age中有重复数据 select distinct post from emp; PS:只要数据中有一个字段数据重复 都无法筛选数据
(5)order by:
作用:
(1)将筛选出来的顺序 按照升序/降序排序
(2)默认排序是升序
例如:
select * from emp order by salary; select * from emp order by salary asc; # 按照升序排列 select * from emp order by salary desc; # 按照降序排列 #先按照age降序排,在年轻相同的情况下再按照薪资升序排 select * from emp order by age desc,salary asc; # 统计各部门年龄在10岁以上的员工平均工资, 并且保留平均工资大于1000的部门, 然后对平均工资进行排序 select post from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary) desc;
(6)limit
作用:限制查询的条目
例如:
select * from emp limit 5; # 查看5条数据 select * from emp limit 5,5; # 从第6条开始 然后查询5条数据 # 查询工资最高的人的详细信息 select * from emp order by salary desc limit 1;
PS:首先按照薪资将整个表给倒叙 然后取一行即可
三:连表查询
作用:多个表一起查询
#建表 create table dep( id int, name varchar(20) ); create table emp( id int primary key auto_increment, name varchar(20), sex enum('male','female') not null default 'male', age int, dep_id int ); #插入数据 insert into dep values (200,'技术'), (201,'人力资源'), (202,'销售'), (203,'运营'); insert into emp(name,sex,age,dep_id) values ('jason','male',18,200), ('egon','female',48,201), ('kevin','male',38,201), ('nick','female',28,202), ('owen','male',18,200), ('jerry','female',18,204) ;
例如:
select * from emp,dep; # 笛卡尔积 将两个表给合并一起 PS:造成大量重复冗余的数据 # 查询员工及所在部门的信息 select * from emp,dep where emp.dep_id = dep.id; # 查询部门为技术部的员工及部门信息 select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';
PS:此种连表方式麻烦
(2)连表查询的方式
(1)inner join(内连接)
(2)left join(左连接)
(3)right join(右连接)
(4)union (全连接)
(3)inner join:
例如:
select * from emp inner join dep on emp.dep_id = dep.id; select * from emp inner join dep on emp.dep_id = dep.id where dep.name = '技术';
PS:
(1)在内连接中其会将on后面的条件作为拼接条件将两个表拼接在一起
(2)其只会将两个表格都有的数据对应拼接起来
(4)left join
例如:
select * from emp left join dep on emp.dep_id = dep.id; select * from emp left join dep on emp.dep_id = dep.id where dep.name = '技术';
PS:
(1)其在内连接的基础上 保留左边表格没有对应关系的数据
(2)没有对应关系的数据 以null进行填充
(5)right join
例如:
select * from emp right join dep on emp.dep_id = dep.id; select * from emp right join dep on emp.dep_id = dep.id where dep.name = '技术';
PS:
(1)其在内连接的基础上 保留右边没有对应关系的数据
(2)没有对应关系的数据 以null进行填充
(6)union
例如:
select * from emp left join dep on emp.dep_id = dep.id union select * from emp right join dep on emp.dep_id = dep.id;
PS:
(1)在内连接的基础上 保留左右两边没有对应关系的数据
(2)没有对应关系的数据 以null进行填充
(7)子查询
作用:一张表的查询结果 作为另一张表的查询条件
例如:
# 获取jason对应的部门 select dep_id from emp where name = 'jason'; # 获取id号 select name from dep where id = '200'; # 获取部门名称 # 拼接 select name from dep where id = (select dep_id from emp where name = 'jason');
四:慢查询
(1)定义:
(1)分析MySQL语句查询性能方法 除了使用EXPLAN输出执行计划
(2)还可以让MySQL下记录查询超过指定时间的语句
(3)我们将超过指定查询时间的SQL语句称之为慢查询

浙公网安备 33010602011771号