书写顺序select id,name from emp where id >3 and id <6;执行顺序from # 先确定是哪张表where # 再确定是否有过滤条件select # 最后确定要过滤出来的数据的哪些字段
# 1.查询id大于等于3小于等于6的数据select id,name from emp where id >= 3 and id <= 6;select * from emp where id between 3 and 6; # 2.查询薪资是20000或者18000或者17000的数据select * from emp where salary = 20000 or salary = 18000 or salary = 17000;select * from emp where salary in (20000,18000,17000); # 简写# 3.查询员工姓名中包含o字母的员工姓名和薪资# 在你刚开始接触mysql查询的时候,建议你按照查询的优先级顺序拼写出你的sql语句"""先是查哪张表 from emp再是根据什么条件去查 where name like ‘%o%’再是对查询出来的数据筛选展示部分 select name,salary"""select name,salary from emp where name like '%o%';# 4.查询员工姓名是由四个字符组成的员工姓名与其薪资select name,salary from emp where name like '____';select name,salary from emp where char_length(name) = 4;# 5.查询id小于3或者大于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.查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用isselect name,post from emp where post_comment = NULL; # 查询为空!select name,post from emp where post_comment is NULL;select name,post from emp where post_comment is not NULL;
# 数据分组应用场景:每个部门的平均薪资,男女比例等# 1.按部门分组select * from emp group by post; # 分组后取出的是每个组的第一条数据select id,name,sex from emp group by post; # 验证
"""设置sql_mode为only_full_group_by,意味着以后但凡分组,只能取到分组的依据,不应该在去取组里面的单个元素的值,那样的话分组就没有意义了,因为不分组就是对单个元素信息的随意获取"""set global sql_mode="strict_trans_tables,only_full_group_by";# 重新链接客户端select * from emp group by post; # 报错select id,name,sex from emp group by post; # 报错select post from emp group by post; # 获取部门信息# 强调:只要分组了,就不能够再“直接”查找到单个数据信息了,只能获取到组名
# 2.获取每个部门的最高工资 # 以组为单位统计组内数据>>>聚合查询(聚集到一起合成为一个结果)# 每个部门的最高工资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;# 3.查询分组之后的部门名称和每个部门下所有的学生姓名# group_concat(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用select post,group_concat(name) from emp group by post;select post,group_concat(name,"_SB") from emp group by post;select post,group_concat(name,": ",salary) from emp group by post;select post,group_concat(salary) from emp group by post;# 4.补充concat(不分组时用)拼接字符串达到更好的显示效果 as语法使用select name as 姓名,salary as 薪资 from emp;select concat("NAME: ",name) as 姓名,concat("SAL: ",salary) as 薪资 from emp;# 补充as语法 即可以给字段起别名也可以给表起select emp.id,emp.name from emp as t1; # 报错 因为表名已经被你改成了t1select t1.id,t1.name from emp as t1;# 查询四则运算# 查询每个人的年薪select name,salary*12 as annual_salary from emp;select name,salary*12 annual_salary from emp; # as可以省略
# 统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门select post,avg(salary) from emp where age >= 30 group by post having avg(salary) > 10000;# 可以将having去掉,查看结果,对比即可验证having用法!#强调:having必须在group by后面使用select * from emp having avg(salary) > 10000; # 报错
select distinct post from emp;
select * from emp order by salary asc; #默认升序排select * from emp order by salary desc; #降序排select * from emp order by age desc; #降序排#先按照age降序排,在年轻相同的情况下再按照薪资升序排select * from emp order by age desc,salary asc; # 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary) ;
select * from emp limit 3;# 查询工资最高的人的详细信息select * from emp order by salary desc limit 1;# 分页显示select * from emp limit 0,5; # 第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置select * from emp limit 5,5;
贪婪匹配与非贪婪匹配:.* | .*?
select * from emp where name regexp '^j.*(n|y)$';
\G 结尾,逐条显示
ps:如果在windows系统中,插入中文字符,select的结果为空白,可以将所有字符编码统一设置成gbk
# 书写顺序
select distinct 字段名1,字段名2,字段名3 from '表名'
where 分组之前的过滤条件
group by 分组依据
having 分组之后的过滤条件
order by 字段1 asc,字段2 desc
limit 5,5 从第五条开始往后展示五条
# 执行顺序 from where group by having order by limit distinct select
9
select * from employee where exists (select id from department where id > 3);select * from employee where exists (select id from department where id > 250);
#建表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 (id,'name'),(...)...;insert into emp(name,sex,age,dep_id) values('name','sex',age,dep_id),(...)...;
select * from emp,dep; # 左表一条记录与右表所有记录都对应一遍>>>笛卡尔积# 将所有的数据都对应了一遍,虽然不合理但是其中有合理的数据,现在我们需要做的就是找出合理的数据# 查询员工及所在部门的信息select * from emp,dep where emp.dep_id = dep.id;# 查询部门为技术部的员工及部门信息select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';# 将两张表关联到一起的操作,有专门对应的方法# 1、内连接:只取两张表有对应关系的记录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 = "技术";# 2、左连接: 在内连接的基础上保留左表没有对应关系的记录select * from emp left join dep on emp.dep_id = dep.id;# 3、右连接: 在内连接的基础上保留右表没有对应关系的记录select * from emp right join dep on emp.dep_id = dep.id;# 4、全连接:在内连接的基础上保留左、右面表没有对应关系的的记录select * from emp left join dep on emp.dep_id = dep.idunionselect * from emp right join dep on emp.dep_id = dep.id;
# 1.查询部门是技术或者人力资源的员工信息"""先获取技术部和人力资源部的id号,再去员工表里面根据前面的id筛选出符合要求的员工信息"""select * from emp where dep_id in (select id from dep where name = "技术" or name = "人力资源");# 2.每个部门最新入职的员工 思路:先查每个部门最新入职的员工,再按部门对应上联表查询select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1inner join(select post,max(hire_date) as max_date from emp group by post) as t2on t1.post = t2.postwhere t1.hire_date = t2.max_date;select * from emp inner join dep on emp.dep_id = dep.id;