码迷,mamicode.com
首页 > 数据库 > 详细

mysql数据操作之多表查询

时间:2018-05-11 20:20:34      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:def   max   表示   creat   连表   order   art   运营   none   

一:介绍

主题:

  多表连接查询

  符合条件连接查询

  子查询

准备表

技术分享图片
#建表
create table department(
id int,
name varchar(20) 
);

create table employee(
id int primary key auto_increment,
name varchar(20),
sex enum(male,female) not null default male,
age int,
dep_id int
);

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

insert into employee(name,sex,age,dep_id) values
(egon,male,18,200),
(alex,female,48,201),
(wupeiqi,male,38,201),
(yuanhao,female,28,202),
(liwenzhou,male,18,200),
(jingliyang,female,18,204)
;


#查看表结构和数据
mysql> desc department;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+

mysql> desc employee;
+--------+-----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+-----------------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| sex | enum(male,female) | NO | | male | |
| age | int(11) | YES | | NULL | |
| dep_id | int(11) | YES | | NULL | |
+--------+-----------------------+------+-----+---------+----------------+

mysql> select * from department;
+------+--------------+
| id | name |
+------+--------------+
| 200 | 技术 |
| 201 | 人力资源 |
| 202 | 销售 |
| 203 | 运营 |
+------+--------------+

mysql> select * from employee;
+----+------------+--------+------+--------+
| id | name | sex | age | dep_id |
+----+------------+--------+------+--------+
| 1 | egon | male | 18 | 200 |
| 2 | alex | female | 48 | 201 |
| 3 | wupeiqi | male | 38 | 201 |
| 4 | yuanhao | female | 28 | 202 |
| 5 | liwenzhou | male | 18 | 200 |
| 6 | jingliyang | female | 18 | 204 |
+----+------------+--------+------+--------+
View Code

二 多表连接查询

重点:外链接语法
SELECT 字段列表
    FROM 表1 INNER|LEFT|RIGHT JOIN 表2
    ON 表1.字段 = 表2.字段;

1、内连接:把两张表有对应关系的记录连接成一张虚拟表

select * from emp inner join dep on emp.dep_id = dep.id;

#应用:
select * from emp,dep where emp.dep_id = dep.id and dep.name = "技术"; # 不要用where做连表的活

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.id
union
select * from emp right join dep on emp.dep_id = dep.id;

 

#补充:多表连接可以不断地与虚拟表连接

查找各部门最高工资
select t1.* from emp as t1
inner join
(select post,max(salary) as ms from emp group by post) as t2
on t1.post = t2.post
where t1.salary = t2.ms
;

三:符合条件连接查询

#示例1:以内连接的方式查询employee和department表,并且employee表中的age字段值必须大于25,即找到年龄大于25岁的员工以及员工所在的部门
select employee.name,department.name from employee inner join department
    on employee.dep_id = department.id
    where age > 25;

#示例2:以内连接的方式查询employee 和department表,并且以age字段的升序方式显示
select employee.id ,employee.name,employee.age,department.name from  employee, department
    where employee.dep_id =department.id
    and age >25
    order by age asc
;

四 子查询

#1、子查询是将一个查询语句嵌套在另一个查询语句中。
#2、内层查询语句的查询结果,可以为外层查询语句提供查询条件。
#3、子查询中可以包含:IN NOT IN ANY ALL EXISTS 和 NOT EXISTS等关键词
#4、还可以包含比较运算符:=、!=、    >、<等

1、带IN关键词的子查询

#子查询:把一个查询语句用括号括起来,当做另一条查询语句的条件去用,称之为子查询

select emp.name from emp inner join dep on emp.dep_id =dep.id where dep.name=技术;

select name from emp where dep_id =
(select id from department where name =技术);
#查询平均年龄在25岁以上的部门名字
select name from dep where id in
(select dep_id  from emp group by dep_id having avg(age)>25);

#查看技术部员工姓名
select name from emp 
    where dep_id in
        (select id from dep where name =技术);
#查看不足1人的部门名(子查询得到的是有人的部门id)
select name from dep where id not in (select distinct dep_id from employee);

#查看每个部门最新入职的那位员工
select t1.id,t1.name,t1.post,t1.hire_date,t2.max_date 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
;

2 带比较运算符的子查询

EXISTS关键词表示存在。在使用EXISTS关键词时,内层查询语句不返回查询的记录。

而是返回一个真假值。TRUE或FALSE

当返回True时,外层查询语句将进行查询;当返回为False时,外层查询语句不进行查询。

判断department表中存在dep_id=203,True
select * from employee
where exists
    (select id from department where id = 200);
+----+------------+--------+------+--------+
| id | name       | sex    | age  | dep_id |
+----+------------+--------+------+--------+
|  1 | egon       | male   |   18 |    200 |
|  2 | alex       | female |   48 |    201 |
|  3 | wupeiqi    | male   |   38 |    201 |
|  4 | yuanhao    | female |   28 |    202 |
|  5 | liwenzhou  | male   |   18 |    200 |
|  6 | jingliyang | female |   18 |    204 |
+----+------------+--------+------+--------+
#department表中存在dept_id=205,False
select * from employee
    where exists 
    (select id from department where id=204);

 

mysql数据操作之多表查询

标签:def   max   表示   creat   连表   order   art   运营   none   

原文地址:https://www.cnblogs.com/wuchenyu/p/9021457.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!