SQL的7种连接查询
在使用数据库查询语句时,单表的查询有时候不能满足项目的业务需求;在项目开发过程中,有很多需求都是要涉及到多表的连接查询。
连接查询:也可以叫跨表查询,需要关联多个表进行查询。
以下通过两表实例来详细介绍连接的使用方式。
department表:
建表语句:
DROP TABLE IF EXISTS department
;CREATE TABLE department
( id
int(11) NOT AUTO_INCREMENT, deptName
varchar(30) DEFAULT , address
varchar(40) DEFAULT , PRIMARY KEY (id
)) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
表数据:
INSERT INTO department
VALUES (‘1’, ‘研发部(RD)’, ‘2层’);
INSERT INTO department
VALUES (‘2’, ‘人事部(HR)’, ‘3层’);
INSERT INTO department
VALUES (‘3’, ‘市场部(MK)’, ‘4层’);
INSERT INTO department
VALUES (‘4’, ‘后勤部(MIS)’, ‘5层’);
INSERT INTO department
VALUES (‘5’, ‘财务部(FD)’, ‘6层’);
employee表:
建表语句:
DROP TABLE IF EXISTS employee
;CREATE TABLE employee
( id
int(11) NOT AUTO_INCREMENT, name
varchar(20) DEFAULT , dep_id
int(11) DEFAULT , age
int(11) DEFAULT , salary
decimal(10,2) DEFAULT , cus_id
int(11) DEFAULT , PRIMARY KEY (id
)) ENGINE=InnoDB AUTO_INCREMENT=109 DEFAULT CHARSET=utf8;
表数据:
INSERT INTO employee
VALUES (‘1’, ‘鲁班’, ‘1’, ‘10’, ‘1000.00’, ‘1’);INSERT INTO employee
VALUES (‘2’, ‘后裔’, ‘1’, ‘20’, ‘2000.00’, ‘1’);INSERT INTO employee
VALUES (‘3’, ‘孙尚香’, ‘1’, ‘20’, ‘2500.00’, ‘1’);INSERT INTO employee
VALUES (‘4’, ‘凯’, ‘4’, ‘20’, ‘3000.00’, ‘1’);INSERT INTO employee
VALUES (‘5’, ‘典韦’, ‘4’, ‘40’, ‘3500.00’, ‘2’);INSERT INTO employee
VALUES (‘6’, ‘貂蝉’, ‘6’, ‘20’, ‘5000.00’, ‘1’);INSERT INTO employee
VALUES (‘7’, ‘孙膑’, ‘6’, ‘50’, ‘5000.00’, ‘1’);INSERT INTO employee
VALUES (‘8’, ‘蔡文姬’, ‘30’, ‘35’, ‘4000.00’, ‘1’);
下面进入正题!
01 内连接
图示:
作用:查询两张表的共有部分
语句:
Select <select_list> from tableA AInner join tableB Bon A.Key = B.Key
原表数据:
示例:
SELECT * from employee eINNER JOIN department don e.depart_id = d.id;
查询结果数据:
02 左连接
图示:
作用:把左边表的内容全部查出,右边表只查出满足条件的记录
语句:
Select <select_list> from tableA ALeft Join tableB Bon A.Key = B.Key
原表数据:
示例:
SELECT * from employee eLEFT JOIN department don e.depart_id = d.id;
查询结果数据:
03 右连接
图示:
作用:把右边表的内容全部查出,左边表只查出满足条件的记录
语句:
Select <select_list> from tableA ALeft Join tableB Bon A.Key = B.Key
原表数据:
示例:
SELECT * from employee eRIGHT JOIN department don e.depart_id = d.id;
查询结果数据:
04 查询左表独有数据
图示:
作用:查询A的独有数据
语句:
Select <select_list> from tableA ALeft Join tableB Bon A.Key = B.Key where B.key IS
原表数据:
示例:
SELECT * from employee eLEFT JOIN department don e.depart_id = d.id WHERE d.id IS ;
查询结果数据:
05 查询右表独有数据
图示:
作用:查询B的独有数据
语句:
Select <select_list> from tableA ARight Join tableB Bon A.Key = B.Key where A.key IS
原表数据:
示例:
SELECT * from employee eRIGHT JOIN department don e.depart_id = d.id WHERE e.id IS ;
查询结果数据:
06 全连接
图示:
作用:查询两个表的全部信息
语句:
Select <select_list> from tableA A Full Outter Join tableB B on A.Key = B.Key
注:Mysql默认不支持此种写法;Oracle支持,在Mysql中可以使用UNION来实现相应操作。
原表数据:
示例:
SELECT * from employee eLEFT JOIN department don e.depart_id = d.id
UNION
SELECT * from employee eRIGHT JOIN department don e.depart_id = d.id
查询结果数据:
07 查询左右表各自的独有的数据
图示:
作用:查询A和B各自的独有的数据
语句:
Select <select_list> from tableA A Full Outter Join tableB B on A.Key = B.Key where A.key = or B.key=
原表数据:
示例:
SELECT * from employee eLEFT JOIN department don e.depart_id = d.id WHERE d.id is
UNION
SELECT * from employee eRIGHT JOIN department don e.depart_id = d.idWHERE e.depart_id is
查询结果数据: