MySQL语句查询

基本查询

一、
书写顺序

select id,name from emp where id > 3;

执行顺序:
from->where->select
二、案例
1、查询id大于等于3小于等于6的数据

select id,name from emp where id >=3 and id <=6;
select id,name 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的员工姓名和薪资

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或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、查询岗位描述为空的员工姓名和岗位名 针对null不用等号 用 is

select name,post from emp where post_comment is null;

分组

1、按照部门分组(下面代码只返回分组之后
每个组的第一条数据。设置严格模式后 则会报错。

select * from emp group by post;

设置严格模式:

set global sql_mode = 'strict_trans_tables,only_full_group_by;

2、拿到分组后的组名

select post from emp group by post;

3、获取每个部门的最高薪资

select post,max(salary) from emp group by post;
select post as '部门',max(salary) as '最高薪资' from emp group by post;

最低薪资:min(salary)

平均薪资:avg(salary)
工资总和:sum(salary)
4、获取每个部门的人数

select post,count(id) from emp group by post;
select post,count(salary) from emp group by post;
select post,count(age) from emp group by post;

5、查询分组之后部门名称和每个部门下所有的员工姓名
group_concat不单单可以支持获取分组之后的其他字段,还支持拼接操作

select post,group_concat(name) from emp group by post;
select post,group_concat(name,'_DSB') from emp group by post;
select post,group_concat(name,':',salary) from emp group by post;
# concat不分组的时候用
select concat('NAME:',name),concat('SAL:',salary) from emp;

6、as语法不单单可以给字段起别名 还可以给表临时起别名

select emp.id,emp.name from emp;
select t1.id,t1.name from emp as t1;

7、查询每个人的年薪

select name,salary*12 from emp;

8、关键字where和group by同时出现的时候group by 必须在where的后面
where先对整体数据进行过滤之后再进行分组
where筛选条件不能使用聚合函数

select id,name from emp where max(salary)>3000;  # 报错
select max(salary) from emp;  # 不分组默认整体就是一组 

9、统计各部门年龄再在30岁以上的员工的平均薪资

select post,avg(salary) from emp where age>30 group by post;

三、having分组之后的筛选条件
having的语法与where是一样的
不过having是在分组之后进行的过滤操作
即having是可以直接使用聚合函数的
1、统计各部门年龄在30岁以上的员工平均薪资并且保留薪资大于10000的部门

select post,avg(salary) from emp
				where age>30
				group by post
				having avg(salary)>10000
				```



四、distinct去重(一定要所有数据不一样才能去重,即一定不要把主键筛选出来)

select distinct age from emp;


五、order by 排序

select * from emp order by salary;  #  默认升序
select * from emp order by salary asc;  # asc可不写
select * from emp order by salary desc;  # 降序
select * from emp order by age desc,salary asc;  # 先按age降序 如果碰到age相同 则再按照salary升序排列




六、limit限制展示条数

select * from emp limit 3;  # 只展示三条数据
select * from emp limit 0,5;
select * from emp limit 5,5;  # 第一个参数是起始位置 第二个参数是展示条数


七、拼表

select * from dep,emp;  # 结果 笛卡尔积
select * from emp,dep where emp.dep_id=dep.id;


1、inner join 内连接

select * from emp inner join dep on emp.dep_id=dep.id; # 只拼接两张表共有的数据


2、left join 左连接

select * from emp left join dep on emp.dep_id=dep.id; # 左表所有的数据都展示出来 没有对应的项就用null填充



3、right join 右连接

select * from emp right join dep on emp.dep_id=dep.id;  # 右表所有的数据都展示出来 没有对应的项就用null填充


4、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;

八、子查询
1、查询部门是技术或者人力资源的员工信息
(涉及了员工表和部门表)
先获取部门的id号
再去员工表里面筛选出对应的员工

select id from dep where name='技术' or name='人力资源';
select * from emp where dep_id in (select id from dep where name='技术' or name='人力资源);

表达查询结果可以作为其他表的查询的条件 也可以通过起别名的方式把它作为一张虚拟表与其他表关联
多表查询就两种方式:
先拼接表再查询
子查询 一步一步来

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值