一.准备工作
先把表建立好,方便一会查询.
create table emp( id int not null unique auto_increment, name varchar(20) not null, sex enum("男","女") not null default "男", age int(3) unsigned not null default 18, hire_date date not null, post varchar(50), post_comment varchar(100), salary double(15,2), office int, depart_id int ); #插入数据 insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values ('egon','male',18,'20170301','老男孩驻沙河办事处外交大使',7300.33,401,1), #以下是教学部 ('alex','male',78,'20150302','teacher',1000000.31,401,1), ('wupeiqi','male',81,'20130305','teacher',8300,401,1), ('yuanhao','male',73,'20140701','teacher',3500,401,1), ('liwenzhou','male',28,'20121101','teacher',2100,401,1), ('jingliyang','female',18,'20110211','teacher',9000,401,1), ('jinxin','male',18,'19000301','teacher',30000,401,1), ('成龙','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) ;
二. 查询语法和语句的优先级(重点)
优先级:
重点中的重点:关键字的执行优先级 from where group by having select distinct order by limit -------------------------------------------------------------------------------------------- 1.找到表:from 2.拿着where指定的约束条件,去文件/表中取出一条条记录 3.将取出的一条条记录进行分组group by,如果没有group by,则整体作为一组 4.将分组的结果进行having过滤 5.执行select 6.去重 7.将结果按条件排序:order by 8.限制结果的显示条数
语法:
select distinct 字段1,字段2,字段3 from 库.表 #去重 where #条件 group #by 分组条件 having #过滤 order by #排序 limit n;# 显示数目
1 1.注意: 2 select * from t1 where 条件 group by 分组字段 3 1.分组只能查询分组字段,要想查看其余的利用聚合函数 4 2.聚合函数的分类:count,min,max,avg,group_concat,sum等。 5 3.模糊匹配:用like关键字。 6 select * from t1 where name like '%eg%'; #%表示任意字符 7 select * from t1 where name like 'd__l'; #一个下划线表示一个字符,两个下划线就表示两个字符 8 4.拷贝表 :create table t2 select * from t1; 9 create table t2 select * from t1 where 1=2 ; #只拷贝表结构
三.简单查询
#查询所有 #1.select * from emp; # 效率不高,测试时可以使用 #2.select name,sex,age,hire_date,post,salary,office,depart_id from emp;#效率比* 高 #查询指定字段的内容 select name,salary from emp; #查看姓名和薪资情况 #查询去重,比如查询有那几个部门 select distinct post from emp; #在被查询字段前面加上distinct #查询时 使用四则运算 select name,salary*12 from emp; #计算个人的年薪 select name as "姓名",salary*12 as "年薪" from emp; #给相应的字段设置别名 # 自定义字段格式,使用concat函数.每个字段都可以使用 select concat("姓名:",name)as "姓名",concat("性别:",sex)as "性别",concat("年薪:",salary *12) as "年薪" from emp; #以分隔符来显示内容如:egon:male:18 这种格式 #方式1 select concat(name,":",sex,":",age) from emp; #方式2 select concat_ws(":",name,sex,age) from emp;
#小练习 """1 查出所有员工的名字,薪资,格式为 <名字:egon> <薪资:3000> 2 查出所有的岗位(去掉重复) 3 查出所有员工名字,以及他们的年薪,年薪的字段名为annual_year """
1 select concat("<","名字:",name,">"," <","薪资:",salary,">")from emp; #1.员工信息 2 select distinct post from emp; #2.职位去重 3 select name as "姓名",salary as annual_year from emp; #3.按格式查找年薪
四. where语句
where字句中可以使用:
1. 比较运算符:> < >= <= <> !=
2. between 80 and 100 值在10到20之间
3. in(80,90,100) 值是10或20或30
4. like 'egon%'
pattern可以是%或_,
%表示任意多字符
_表示一个字符
5. 逻辑运算符:在多个条件直接可以使用逻辑运算符 and or not
#1.单条件查询 #1.查找所有的销售员 select id,name,age from emp where post='sale'; #2.查找工资大于8000的讲师 select id,name,salary from emp where salary >=8000 and post="teacher"; #3,查找薪资大于等于20000,小于等于30000的 #select name,post,salary from emp where 20000<=salary<=30000;# 不支持这样写 select name,post,salary from emp where salary >=20000 and salary <=30000; select name,post,salary from emp where salary between 20000 and 30000; #4.查找薪资小于20000 或大于30000的 select name,post,salary from emp where salary <20000 or salary >30000; select name,post,salary from emp where salary not between 20000 and 30000; #取反.刚好不在20000-30000这个范围内 #5.查找年龄是81,73,48的人 select name,age from emp where age=81 or age=73 or age=48; select name,age from emp where age in(81,73,48); #6.查找描述信息是否为null select * from emp where post_comment is NULL; #7.查询名字是jin开头的员工 select name from emp where name like"jin%"; #记得加%,表示任意长度的任意字符 select name from emp where name like"jin_"; #下划线表示一个字符,就是说字符长度为4个.
小练习:
1. 查看岗位是teacher的员工姓名、年龄
2. 查看岗位是teacher且年龄大于30岁的员工姓名、年龄
3. 查看岗位是teacher且薪资在9000-1000范围内的员工姓名、年龄、薪资
4. 查看岗位描述不为NULL的员工信息
5. 查看岗位是teacher且薪资是10000或9000或30000的员工姓名、年龄、薪资
6. 查看岗位是teacher且薪资不是10000或9000或30000的员工姓名、年龄、薪资
7. 查看岗位是teacher且名字是jin开头的员工姓名、年薪
1. select name,age from emp where post="teacher"; 2. select name,age from emp where age >30; 3. select name,age from emp where salary between 9000 and 10000; 4. select * from emp where post_comment is not null; 5. select name,age,salary from emp where post="teacher" and salary in(10000,9000,30000); 6. select name,age,salary from emp where post ="teacher" and salary not in(10000,9000,30000); 7. from emp where post="teacher" and name like"jin%";
五. group by(分组查询)
什么是分组,为什么要分组?
#1、首先明确一点:分组发生在where之后,即分组是基于where之后得到的记录而进行的 #2、分组指的是:将所有记录按照某个相同字段进行归类,比如针对员工信息表的职位分组,或者按照性别进行分组等 #3、为何要分组呢? 取每个部门的最高工资 取每个部门的员工数 取男人数和女人数 小窍门:‘每’这个字后面的字段,就是我们分组的依据 #4、大前提: 可以按照任意字段分组,但是分组完毕后,比如group by post,只能查看post字段,如果想查看组内信息,需要借助于聚合函数
注意:如果我们用unique的字段作为分组的依据,则每一条记录自成一组,这种分组没有意义,多条记录之间的某个字段值相同,该字段通常用来作为分组的依据
六. ONLY_FULL_GROUP_BY,聚合函数
1 #设置 2 set global sql_mode = "ONLY_FULL_GROUP_BY"; 3 #取消 4 set global sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
聚合函数:
max,min,sum,count,avg,group_concat
#强调:聚合函数聚合的是组的内容,若是没有分组,则默认一组 示例: SELECT COUNT(*) FROM emp; SELECT COUNT(*) FROM emp WHERE depart_id=1; SELECT MAX(salary) FROM emp; SELECT MIN(salary) FROM emp; SELECT AVG(salary) FROM emp; SELECT SUM(salary) FROM emp; SELECT SUM(salary) FROM emp WHERE depart_id=3;
SELECT POST GOURP_CONCAT(name) FROM emp GROUP BY post; #格式化输出
小练习:
1. 查询岗位名以及岗位包含的所有员工名字 2. 查询岗位名以及各岗位内包含的员工个数 3. 查询公司内男员工和女员工的个数 4. 查询岗位名以及各岗位的平均薪资 5. 查询岗位名以及各岗位的最高薪资 6. 查询岗位名以及各岗位的最低薪资 7. 查询男员工与男员工的平均薪资,女员工与女员工的平均薪资
1.select post,group_concat(name) from emp group by post; 2.select post,count(id) from emp group by post; 3.select sex,count(id) from emp group by sex ; 4.select post,avg(salary) from emp group by post; 5.select post,max(salary) from emp group by post; 6.select post,min(salary) from emp group by post; 7.select sex,avg(salary) from emp group by sex;