基本查询语句及方法

基本查询语句及方法

查询的出的表至关于Excel中刷选操做,刷选出的是虚拟的表,展现出来mysql

一、查询语句的基本操做

  • select
  • from
  • where
  • group by
  • having
  • distinct
  • order by
  • limit
  • 聚合函数:count,max,min,avg,sum

二、单表查询

select * from emp; # 若数据比较多,比较凌乱,能够在表后面+ \Gsql

select * from emp \G编程

写SQL语句必须遵循两点:windows

书写顺序:函数

​ selectpost

​ from编码

​ wherecode

执行顺序:regexp

​ from排序

​ where

​ select

一、where :约束条件

# 建立一张部门表
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),
  office int, # 一个部门一个屋子
  depart_id int
);


# 插入记录
# 三个部门:教学,销售,运营
insert into emp(name,sex,age,hire_date,post,salary,office,depart_id) values
('Mr沈','male',17,'20170301','admin',7300.33,401,1), # 如下是教学部
('jerry','female',18,'20110211','teacher',9000,401,1),
('tank','male',18,'19000301','teacher',30000,401,1),
('sean','male',48,'20101111','teacher',10000,401,1),

('歪歪','female',48,'20150311','sale',7000.13,402,2),# 如下是销售部门
('丫丫','female',38,'20101101','sale',5000.35,402,2),
('丁丁','female',18,'20110312','sale',8000.37,402,2),
('星星','female',18,'20160513','sale',6000.29,402,2),
('格格','female',28,'20170127','sale',4000.33,402,2),

('程咬金','male',28,'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);

# PS:若是在windows系统中,插入中文字符,select的结果为空白,能够将全部字符编码统一设置成gbk

一、查询id大于等于3,小于等于6的数据(and、between)

select * from emp where id >= 3 and id <= 6;
select * from emp where id between 3 and 6;

二、查询薪资是20000或者18000或者17000的数据(or、in)

select * from emp where salary=20000 or salary=18000 or salary=17000;
select * from emp where salary in (20000,18000,17000);

三、查询员工姓名中包含e字母 的 员工姓名 和 薪资

模糊匹配:like

%:匹配0个或多个任意字符

_:匹配一个任意字符

select name,salary from emp where name like '%e%';

四、查找名字个数为4个的员工 名字 与 薪资

char_length(name):计算名字的字符长度

select name,salary from emp where name like '____';
select name,salary from emp where char_length(name)=4;

五、查询id小于3或者大于6的数据(not in)

select * from emp where id not in (3,4,5,6);
select * from emp where id not between 3 and 6;

六、查询岗位描述为空的 员工名 与 岗位名 post_comment

针对查找null的值须要用is,若是用=会查不到数据

select name,post from emp where post_comment is null;

二、group by

书写顺序:select - from - where - group by

执行顺序:from - where - group by - select

一、根据部门分组

非严格模式下能够获取分组之外的字段数据,严格模式会报错

select post, salary from emp group by post;

设置严格模式:

查看模式:show variables like "%mode%";

全局设置严格模式: set global sql_mode="strict_trans_tables,only_full_group_by";

在严格模式下,只能获取分组内 的字段,能够同聚合函数一块儿使用,间接获取其余字段数据

聚合函数:必须跟在group by 后面(执行顺序)

​ count():计数,括号中能够填任意非空值

​ max():最大值

​ min():最小值

​ avg():平均值

​ sum():求和

二、获取每一个部门的最高工资

as :别名,能够给字段加一个别名,能够简写不写as,可是不推荐使用

select post,max(salary) from emp group by post;
select post as '部门',max(salary) as '最高薪资' from emp group by post;
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;

七、查询 "岗位名" 以及 岗位包含的全部员工名字 一行展现

group_concat(name):能够将分组后的全部名字获取并进行拼接,默认以 ,拼接

能够设置指定以什么拼接

select post,group_concat(name) from emp group by post;
select post,group_concat('名字:',name) from emp group by post;

八、统计各部门年龄在30岁如下的员工平均工资

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

三、having:过滤

having与where的做用是同样的,having必须跟在group by后面,having后面能够跟聚合函数,可是where后面不能跟聚合函数

书写顺序:

​ select - from - where - group by - having

执行顺序:

​ from - where - group by - having - select

统计各部门年龄在30岁以上的员工平均工资,而且保留平均工资大于6000的部门

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

四、distinct:去重

书写顺序:

​ select - distinct - from - where - group by - having

执行顺序:

​ from - where - group by - having - distinct - select

distinct 后面跟着去重的字段名

注意:去重的字段名,必须是重复的,只要有不重复的字段,后续就没法去重

将重复的部门数据去重

select distinct post from emp;
select distinct post,id from emp;  # id没有重复的,因此都不会去重

五、order by :排序

书写顺序:

​ select - from - where - group by - having - order by

执行顺序:

​ from - where - group by - having - select - order by

asc:升序

desc:降序

根据薪资进行降序

select salary from emp order by salary desc;

统计各部门年龄在20岁以上的员工平均工资,而且保留平均工资大于7000的部门,而后对平均工资进行升序

select post,avg(salary) from emp where age>20 group by post having avg(salary)>7000 order by avg(salary) asc;

六、limit :限制查询记录的数量

书写顺序:

​ select - from - order by - limit

执行顺序:

​ from - select - order by - limit

limit能够有两个参数,参数1:是限制的开始位置+1,参数2:是开始位置展现的条数

一、获取从第一条开始,获取4条数据

select * from emp limit 4;

二、查询从第五条开始,获取4条数据

select * from emp limit 4,4;

三、查询工资最高的人的详细信息

select * from emp order by salary desc limit 1;

七、regexp:正则(编程中凡是看到reg开头,基本都是与正则有关)

查询名字中以程开头,金银铜铁结尾的名字全部信息

select * from emp where name regexp '^程.*(金|银|铜|铁)$';

三、多表查询

建立表

#建表dep2
create table dep2(
id int,
name varchar(20) 
);

#建表emp2
create table emp2(
id int primary key auto_increment,
name varchar(20),
sex enum('male','female') not null default 'male',
age int,
dep_id int
);

#插入数据
insert into dep2 values
(200,'技术'),
(201,'人力资源'),
(202,'销售'),
(203,'运营');

insert into emp2(name,sex,age,dep_id) values
('小王','male',17,200),
('小李','female',48,201),
('小张','male',38,201),
('小沈','female',28,202),
('狗蛋','male',18,200),
('丫丫','female',18,204);

上篇讲了如何根据表关系对字段进行拆分,目的是为了更好的管理,表数据都存放在硬盘中,存不是目的,目的是为了取,因此咱们将数据从硬盘读到内存中,咱们将其拼接查询的更加的合理

左表的一条记录与右表一条记录都对应,通常称为 “ 笛卡尔积 ”

左右表中的数据基本不可能所有一一对应的,因此咱们能够提取出有对应关系,合理的数据,咱们所须要的数据便可,就有了将两张表关联到一块儿的方法

一、inner join 内链接:只取两张表有对应关系的记录

​ 一、查询员工以及所在部门的信息

select * from emp2 inner join dep2 on emp2.dep_id = dep2.id;

​ 二、查询部门为技术部的员工及部门信息

select * from emp2 inner join dep2 on emp2.dep_id = dep2.id and dep2.name='技术';

二、left join 左链接:在内链接的基础上保留左表没有对应关系的记录

链接保留左表中全部的数据,右表没有与之对应的就会以null补充

select * from emp2 left join dep2 on emp2.dep_id = dep2.id;

三、right join 右链接:在内链接的基础上保留右表全部的数据

保留右表全部的数据,左表没有的数据会以null填充

select * from emp2 right join dep2 on emp2.dep_id = dep2.id;

四、union 全链接:在内链接的基础上保留左右俩表全部的数据,没有与之对应的会以null补充

select * from emp2 left join dep2 on emp2.dep_id = dep2.id
union
select * from emp2 right join dep2 on emp2.dep_id = dep2.id;

四、子查询

子查询就是将一个查询语句的结果用括号括起来,当作另外一个查语句的条件去用

​ 一、查询部门是技术或者人力资源的员工信息

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

​ 二、查询每一个部门最新入职的员工

  1. 先子查询获取emp表中 部门名称与最新入职的时间字段值,生成一张虚拟表
  2. 拼接emp表和刚生成的部门名与最新入职的时间字段值
  3. 判断两张表的时间相等的
  4. 查询出所需的全部信息
select t1.* from
emp as t1 inner join
(select post,max(hire_date) as max_date from emp group by post) as t2
on t1.post = t2.post
where t1.hire_date = t2.max_date;
相关文章
相关标签/搜索