mysql multi-table query, full connection inside and outside (1)

1. Create a department table:

CREATE TABLE dept (
id INT ( 11 ) NOT NULL PRIMARY KEY,
name VARCHAR ( 50 )

);

Add data: 5 departments

INSERT INTO dept ( id, name )
VALUES
	( 1, '销售部' ),
	( 2, '研发部' ),
	( 3, '运营部' ),
	( 4, '人力资源部' ),
	( 5, '产品部' )

2. Create an employee table

CREATE TABLE emp (
id INT ( 11 ) NOT NULL PRIMARY KEY,
name VARCHAR ( 50 ),
entryTime TIMESTAMP,
dept_id INT,
salary DOUBLE
);

Add employee data:

INSERT INTO emp ( id, name, entryTime, dept_id, salary )
VALUES
	( 1, '张飞', '2016-08-16 14:32:08', 1, '8000' ),
	( 2, '关羽', '2016-08-16 14:32:08', 3, '9000' ),
	( 3, '赵云', '2016-08-16 14:32:08', 4, '1000' ),
	( 4, '吕布', '2016-08-16 14:32:08', 2, '2000' ),
	( 5, '曹操', '2016-08-16 14:32:08', 3, '3000' )

Start query operation:

Query single:

1.

select * from dept
select * from emp

Showcase:

2. Query 2 tables

select * from dept,emp

Classification of multi-table query:

(1) Internal query

         (1) Implicit inlining: use where conditions to eliminate useless data

select * from dept,emp WHERE emp.dept_id=dept.id

    Query the specified field: 

select emp.id,emp.name,emp.salary,dept.name
from dept,emp WHERE emp.dept_id=dept.id

           (2) Displayed internal connection

grammar:

select [字段] from [表1] inner join [表2] on [条件]
select * from emp INNER JOIN dept ON emp.dept_id=dept.id

result:

 

(1) External link query

Add a piece of data in the employee table

(1) Left outer connection

The left table is the main table, the left table shows all; the right is the sub table, and the right side shows null when there is no symbol data, and does not display the non-compliant ones;

All the data in the left table and the intersection of the right table

select e1.*,d2.name from emp e1 LEFT OUTER JOIN dept d2 ON e1.dept_id=d2.id

 

(2) Right outer connection

The right side is the main table, the right table shows all; the left is the sub table, and the left side shows null when there is no symbol data, and the non-compliant ones are not displayed;

select e1.*,d2.name from emp e1 RIGHT OUTER JOIN dept d2 ON e1.dept_id=d2.id

select e1.*,d2.name from dept d2 RIGHT JOIN emp e1 ON e1.dept_id=d2.id

(2) Full external connection

Full external connection: the main table on the left, the auxiliary table on the right, the main table and the auxiliary table are all displayed, the right shows null when there is no symbol data, and the left shows null when there is no symbol data, and the data that meets the conditions will be displayed in one row;

MYSQL does not support full outer joins and is suitable for Oracle and DB2. )

In MySQL, you can achieve full outer joins by finding the collection of left outer joins and right outer joins.

SELECT
	emp.`name`,
	emp.dept_id 
FROM
	emp
	LEFT JOIN dept ON dept.id = emp.dept_id UNION
SELECT
	emp.`name`,
	dept.id 
FROM
	emp
	RIGHT JOIN dept ON dept.id = emp.dept_id 

result:

Subquery:

Query the maximum salary:

SELECT max( salary ) FROM emp

Query employees equal to 9000:

SELECT salary FROM emp WHERE emp.salary = 9000

Query the person with the highest salary in the employee table:

SELECT * FROM emp WHERE emp.salary = ( SELECT max( salary ) FROM emp ) 

 

 

 

 

Guess you like

Origin blog.csdn.net/jack_bob/article/details/106822404