学习了mysql 连接查询和子查询,把连接查询的所有情况,做一个介绍。
1,数据:
部门数据:
DROP TABLE IF EXISTS department;
CREATE TABLE department (
id int(11) NOT NULL AUTO_INCREMENT,
deptName varchar(30) DEFAULT NULL,
address varchar(40) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO department VALUES
('1', '研发部(RD)', '2层'),
('2', '人事部(HR)', '3层'),
('3', '市场部(MK)', '4层'),
('4', '后勤部(MIS)', '5层'),
('5', '财务部(FD)', '6层');
员工数据:
DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
id int(11) NOT NULL AUTO_INCREMENT,
name varchar(20) DEFAULT NULL,
dep_id int(11) DEFAULT NULL,
age int(11) DEFAULT NULL,
salary decimal(10,2) DEFAULT NULL,
cus_id int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT INTO employee VALUES
('1', '张三', '1', '10', '1000.00', '1'),
('2', '李四', '1', '20', '2000.00', '1'),
('3', '王五', '1', '20', '2500.00', '1'),
('4', '赵六', '4', '20', '3000.00', '1'),
('5', '孙七', '4', '40', '3500.00', '2'),
('6', '周八', '6', '20', '5000.00', '1'),
('7', '吴九', '6', '50', '5000.00', '1'),
('8', '郑十', '30', '35', '4000.00', '1');
2,内连接
作用: 查询两张表的共有部分,取交集
语法:
SELECT <select_list> FROM tableA A
INNER JOIN tableB B ON A.Key = B.Key;
例子:
SELECT * FROM employee e
INNER JOIN department d ON e.dep_id = d.id;
3,左连接
作用: 把左边表的内容全部查出,右边表只查出满足条件的记录
语法:
SELECT <select_list> FROM tableA A
LEFT JOIN tableB B ON A.Key = B.Key;
例子:
SELECT * FROM employee e
LEFT JOIN department d ON e.dep_id = d.id;
左连接的,右边的没匹配到值,显示为null
4,右连接
作用: 把右边表的内容全部查出,左边表只查出满足条件的记录
语法:
SELECT <select_list> FROM tableA A
RIGHT JOIN tableB B ON A.Key = B.Key;
例子:
SELECT * FROM employee e
RIGHT JOIN department d ON e.dep_id = d.id;
右连接的,左边的没匹配到值,显示为null
5,查询左表独有数据
作用: 查询A的独有数据,取差集, A - B
语法:
SELECT <select_list> FROM tableA A
LEFT JOIN tableB B ON A.Key = B.Key WHERE B.key IS NULL;
例子:
SELECT * FROM employee e
LEFT JOIN department d ON e.dep_id = d.id WHERE d.id IS NULL;
6,查询右表独有数据
作用: 查询B的独有数据,取差集,B - A
语法:
SELECT <select_list> FROM tableA A
RIGHT JOIN tableB B ON A.Key = B.Key WHERE A.key IS NULL;
例子:
SELECT * FROM employee e
RIGHT JOIN department d ON e.dep_id = d.id WHERE e.id IS NULL;
7,全连接
作用: 查询两个表的全部信息,取并集
语法:
SELECT <select_list> FROM tableA A
LEFT JOIN tableB B ON A.Key = B.Key
UNION
SELECT <select_list> FROM tableA A
RIGHT JOIN tableB B ON A.Key = B.Key;
Oracle写法:
SELECT <select_list> FROM tableA A
FULL Outter JOIN tableB B ON A.Key = B.Key;
例子:
SELECT * from employee e
LEFT JOIN department d on e.dep_id = d.id
UNION
SELECT * from employee e
RIGHT JOIN department d on e.dep_id = d.id;
8,查询左右表各自的独有的数据
作用: 查询A和B各自的独有的数据
语法:
SELECT <select_list> FROM tableA A
LEFT JOIN tableB B ON A.Key = B.Key WHERE B.key IS NULL
UNION
SELECT * FROM employee e
RIGHT JOIN department d ON e.dep_id = d.id WHERE e.id IS NULL;
oracle的话:
SELECT <select_list> FROM tableA A
FULL Outter JOIN tableB B ON A.Key = B.Key
WHERE A.key = NULL OR B.key=NULL;
例子:
SELECT * FROM employee e
LEFT JOIN department d ON e.dep_id = d.id WHERE d.id IS NULL
UNION
SELECT * FROM employee e
RIGHT JOIN department d ON e.dep_id = d.id WHERE e.dep_id IS NULL;
总结:
在连接的各情况中平时等级连接比较多,其次是左右连接,其它的相对比较少,就看实际的要求了。
上一篇: 《mysql 连接查询和子查询》
下一篇: 《mysql 正则表达查询》