Skip to content

查询关键字详解

1. 关键字

where 筛选

刚开始接触mysql查询的时候,建议按照查询的优先级顺序拼写出sql语句

1.先是查哪张表 from emp2.再是根据什么条件去查 where id = 63.再是对查询出来的数据筛选展示部分 select name,salary

  1. 查询id大于等于3小于等于6的数据(两种方式)
sql
select id,name from emp where id >= 3 and id <= 6;

select id,name from emp where id between 3 and 6;
  1. 查询薪资是20000或者18000或者17000的数据(两种方式)
sql
select * from emp where salary = 20000 or salary = 18000 or salary = 17000;

select * from emp where salary in (20000,18000,17000);
  1. 查询员工姓名中包含o字母的员工姓名和薪资
sql
select name,salary from emp where name like '%o%';
  1. 查询员工姓名是由四个字符组成的员工姓名与其薪资(两种方式)
sql
select name,salary from emp where name like '____';

select name,salary from emp where char_length(name) = 4;
  1. 查询id小于3或者大于6的数据
sql
select * from emp where id not between 3 and 6;
  1. 查询薪资不在20000,18000,17000范围的数据
sql
select * from emp where salary not in (20000,18000,17000);
  1. 查询岗位描述为空的员工名与岗位名 针对null不能用等号,只能用is
sql
select 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;

group by 分组

分组:按照一些指定的条件将单个单个的数据分为一个个整体

分组之后我们研究的对象应该是以组为单位 不应该再直接获取单个数据项 如果获取了应该直接报错 select后面可以直接填写的字段名只能是分组的依据(其他字段需要借助于一些方法才可以获取)

sql
set global sql_mode='strict_trans_tables,only_full_group_by';

我们写SQL是否需要使用分组 可以在题目中得到答案   每个、平均、最大、最小

配置分组常见使用的有聚合函数

序号关键字作用
1.max最大值
2.min最小值
3.sum总和
4.counnt计数
5.avg平均值
  1. 每个部门的最高薪资
sql
select post,max(salary) from emp group by post;
  1. 每个部门的最低薪资
sql
select post,min(salary) from emp group by post;
  1. 每个部门的平均薪资
sql
select post,avg(salary) from emp group by post;
  1. 每个部门的工资总和
sql
select post,sum(salary) from emp group by post;
  1. 每个部门的人数
sql
select post,count(salary) from emp group by post;
  1. 在显示的时候还可以给字段取别名 as也可以省略 但是不推荐省 因为寓意不明确
sql
select post as '部门',max(salary) as '最高薪资' from emp group by post;

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

group_conca(分组之后用)不仅可以用来显示除分组外字段还有拼接字符串的作用

  1. 每个人的名字
sql
select post,group_concat(name) from emp group by post;
  1. 添加后缀
sql
select post,group_concat(name,'_NB') from emp group by post;
  1. 拼接字符串
sql
select post,group_concat(name,'薪资为: ',salary) from emp group by post;
  1. 每个人的薪资
sql
select post,group_concat(salary) from emp group by post;

having 过滤

where与having的功能其实是一样的 都是用来筛选数据 只不过where用于分组之前的筛选 而having用于分组之后的筛选 为了人为的区分 所以叫where是筛选 having是过滤

  1. 统计各部门年龄在30岁以上的员工平均工资,并且保留平均工资大于10000的部门
sql
select post,avg(salary) from emp where age > 30 group by post having avg(salary) > 10000;

distinct 去重

去重的前提是数据必须是一模一样

sql
select distinct age from emp;

order by 排序

sql
--- 升序(默认的)
select * from emp order by salary asc;

--- 降序
select * from emp order by salary desc;

案例: 统计各部门年龄在10岁以上的员工平均工资,并且保留平均工资大于1000的部门,然后对平均工资进行排序

sql
select post,avg(salary) from emp where age > 10 group by post having avg(salary) > 1000 order by avg(salary);

limit 分页

1.限制展示条数

sql
select * from emp limit 3;

2.分页展示

第一个参数表示起始位置,第二个参数表示的是条数,不是索引位置

sql
select * from emp limit 5,5;

regexp 正则

sql
select * from emp where name regexp'^j.*(ny)$';

2. 实践

多表查询

现将所有涉及到结果的表全部拼接到一起形成一张大表 然后从大表中查询数据

sql
--- 建表
create table dep1(
    id int primary key auto_increment,
    name varchar(20) 
);
      
create table emp1(
    id int primary key auto_increment,
    name varchar(20),
    gender enum('male','female') not null default 'male',
    age int,
    dep1_id int
);

--- 插入数据
insert into dep1 values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营'),
(205,'安保');

insert into emp1(name,gender,age,dep1_id) values
('jason','male',18,200),
('lisa','female',48,201),
('kevin','male',18,201),
('oscar','male',28,202),
('tony','male',18,203),
('jerry','female',18,204);

子查询

将一张表查询结果用括号括起来当做另外一条SQL语句的条件 eg: 第一步干什么 第二步基于第一步的结果在做操作

sql
--- 查询jason的部门名称

--- 1.先获取jason的部门编号
select dep1_id from emp1 where name = 'jason';  # 200

--- 2.根据部门编号获取部门名称
select name from dep1 where id = 200;

--- 3.子查询
select name from dep1 where id = (select dep1_id from emp1 where name = 'jason');

连表操作

sql
--- 笛卡尔积
select * from emp1,dep1;

'大多数情况下,不会使用笛卡尔积来求数据 效率太低 连表有专门的语法'
  1. inner join内连接
sql
--- 作用:只能拼接两边都有的字段

select * from emp1 inner join dep1 on emp1.dep1_id = dep1.id;
  1. left join左连接
sql
--- 作用:以左表为基准 展示所有的数据 没有对应数据则用NULL填充

select * from emp1 left join dep1 on emp1.dep1_id = dep1.id;
  1. right join 右连接
sql
--- 作用:以右表为基准 展示所有的数据 没有对应数据则用NULL填充

select * from emp1 right join dep1 on emp1.dep1_id = dep1.id;
  1. union 全连接
sql
--- 作用:左连接加右连接 左右两边的数据都有 没有数据的部分用NULL填充

select * from emp1 left join dep1 on emp1.dep1_id = dep1.id 
union 
select *from emp1 right join dep1 on emp1.dep1_id = dep1.id;

like 模糊查询

sql
--- 模糊查询,'%__%'通配符
SELECT  * FROM `employees` WHERE `first_name` LIKE '%a%';

--- 查询员工的名字中第二个字符为_的员工名
SELECT  `last_name` FROM  `employees` WHERE `last_name` LIKE '_\_%';

between and 判断值是否在指定范围内

sql
--- 查询员工编号在100到120之间的员工信息
SELECT * FROM `employees` WHERE `employee_id` BETWEEN 100 AND 120;

in 查询范围内的数据

等效与多个or语句

sql
--- 查询员工的工种编号,AD_VP、 AD_ASST、FI_MGR中的一个员工名和工种编号
SELECT `last_name`, `job_id` FROM  `employees` WHERE `job_id` IN('AD_VP', 'AD_ASST','FI_MGR');

is null 空值查询

sql
--- 查询没有奖金的员工名和奖金率
SELECT `last_name`,`commission_pct` FROM `employees` WHERE `commission_pct` IS NULL;

is not null

sql
--- 查询有奖金的员工名和奖金率
SELECT `last_name`,`commission_pct` FROM `employees` WHERE `commission_pct` IS NOT NULL;

本文作者:许怀安
创作时间:2023.02.11
版权声明:本博客所有文章除特别声明外,均采用BY-NC-SA许可协议。转载请禀明出处!

许怀安 | MIT License