MySQL study notes eight: multi-table query

Create two tables: dept table and emp table 

/*创建部门表*/
CREATE TABLE dept(
	deptno		INT 	PRIMARY KEY,
	dname		VARCHAR(50),
	loc 		VARCHAR(50)
);

/*创建雇员表*/
CREATE TABLE emp(
	empno		INT 	PRIMARY KEY,
	ename		VARCHAR(50),
	job		VARCHAR(50),
	mgr		INT,
	hiredate	DATE,
	sal		DECIMAL(7,2),
	COMM 		DECIMAL(7,2),
	deptno		INT,
	CONSTRAINT fk_emp FOREIGN KEY(mgr) REFERENCES emp(empno)
);
/*插入dept表数据*/
INSERT INTO dept VALUES (10, '教研部', '北京');
INSERT INTO dept VALUES (20, '学工部', '上海');
INSERT INTO dept VALUES (30, '销售部', '广州');
INSERT INTO dept VALUES (40, '财务部', '武汉');

/*插入emp表数据*/
INSERT INTO emp VALUES (1009, '曾阿牛', '董事长', NULL, '2001-11-17', 50000, NULL, 10);
INSERT INTO emp VALUES (1004, '刘备', '经理', 1009, '2001-04-02', 29750, NULL, 20);
INSERT INTO emp VALUES (1006, '关羽', '经理', 1009, '2001-05-01', 28500, NULL, 30);
INSERT INTO emp VALUES (1007, '张飞', '经理', 1009, '2001-09-01', 24500, NULL, 10);
INSERT INTO emp VALUES (1008, '诸葛亮', '分析师', 1004, '2007-04-19', 30000, NULL, 20);
INSERT INTO emp VALUES (1013, '庞统', '分析师', 1004, '2001-12-03', 30000, NULL, 20);
INSERT INTO emp VALUES (1002, '黛绮丝', '销售员', 1006, '2001-02-20', 16000, 3000, 30);
INSERT INTO emp VALUES (1003, '殷天正', '销售员', 1006, '2001-02-22', 12500, 5000, 30);
INSERT INTO emp VALUES (1005, '谢逊', '销售员', 1006, '2001-09-28', 12500, 14000, 30);
INSERT INTO emp VALUES (1010, '韦一笑', '销售员', 1006, '2001-09-08', 15000, 0, 30);
INSERT INTO emp VALUES (1012, '程普', '文员', 1006, '2001-12-03', 9500, NULL, 30);
INSERT INTO emp VALUES (1014, '黄盖', '文员', 1007, '2002-01-23', 13000, NULL, 10);
INSERT INTO emp VALUES (1011, '周泰', '文员', 1008, '2007-05-23', 11000, NULL, 20);
INSERT INTO emp VALUES (1001, '甘宁', '文员', 1013, '2000-12-17', 8000, NULL, 20);

 1. Classification of multi-table query

  • Combine the result set
  • Connect query
  • Subquery

2. Merge the result set

2.1 Function: Combining the result set is to merge the query results of two select statements together! In the table to be merged, the column type and the number of columns are the same!

2.2 There are two ways to merge result sets:

  • UNION: remove duplicate records, for example: SELECT * FROM t1 UNION SELECT * FROM t2;
  • UNION ALL: Do not remove duplicate records, for example: SELECT * FROM t1 UNION ALL SELECT * FROM t2
/*创建t1表*/
create table t1(
name varchar(20),
age int
);

/*添加数据*/
insert into t1
values
('张三',20),
('李四',25),
('王五',22);
/*创建t2表*/
create table t2(
name varchar(20),
age int
);

/*添加数据*/
insert into t2
values
('zs',30),
('ls',25),
('张三',20);

3. Connect query

Join query is to find the product of multiple tables, for example, t1 joins t2, then the result of the query is t1*t2.

Join query will produce Cartesian product, suppose set A={a,b}, set B={0,1,2}, then the Cartesian product of the two sets is {(a,0),(a,1) ,(a,2),(b,0),(b,1),(b,2)}. Can be extended to multiple sets.

So multi-table query to produce such results is not what we want, so how to remove duplicate and unwanted records? You can remove useless information by using primary and foreign keys! 

3.1 Internal connection

  • Dialect: SELECT * FROM Table 1 alias 1, Table 2 alias 2 WHERE alias 1.xx=alias 2.xx
  • Standard: SELECT * FROM table 1 alias 1 INNER JOIN table 2 alias 2 ON alias 1.xx=alias 2.xx
  • Natural: SELECT * FROM table 1 alias 1 NATURAL JOIN table 2 alias 2

 3.1.1 SQL dialect

3.1.2 SQL standard inner join

 3.1.3 Natural connection

3.2 Outer connection

3.2.1 Classification

  • Left outer join
  • Right outer join
  • Full external connection (MySQL does not support)

3.2.2 Left outer connection

The left outer join is to first query the left table (that is, the left table is the main one), and then query the right table. The right table shows that the conditions are met, and the ones that do not meet the conditions are displayed as NULL.

Among them, in the record of "Zhang San" in the emp table, the department number is 50, and there is no record with the department number of 50 in the dept table, so the record of "Zhang San" cannot satisfy the condition of e.deptno=d.deptno . But in the left outer join, because the emp table is the left table, the records in the left table will be queried, that is, the record "Zhang San" will also be found, but the corresponding right table part shows NULL!

3.2.3 Right outer connection

The right outer join is to first query all the records in the right table, and then the left table satisfies the condition of the display, and does not meet the display NULL. For example, there is no employee in the department numbered 40 in the dept table, but in the right connection, if the dept table is the right table, then department 40 will still be found, but the corresponding employee information is NULL. 

3.3 Natural connection (belonging to a simplified way)

4. Subquery

A subquery is a nested query, that is, the SELECT contains SELECT. If there are two or more SELECTs in a statement, then it is a subquery statement.

4.1 Where does the subquery appear:

  • After where, as part of the condition;
  • After from, as a table to be queried;

4.2 When a subquery appears as a condition after where, the following keywords can also be used:

  • any
  • all

4.3 The form of the result set of the subquery:

  • Single row and single column (for conditions)
  • Single row and multiple columns (for conditions)
  • Multiple rows and single column (for conditions)
  • Multiple rows and multiple columns (for tables)

5. Subquery exercises

5.1 Employees whose wages are higher than Ganning

  • Subquery as a condition
  • The subquery form is single row and single column

5.2 Information on employees whose salaries are higher than 30 department owners

The first way:

  • Subquery as a condition
  • The subquery form is multi-row and single-column (when the sub-query result set form is multi-row and single-column, the ALL or ANY keywords can be used)

The second way:

  • Subquery as a condition
  • The subquery form is single row and single column

5.3 Query information about employees whose job and salary are exactly the same as those of Yin Tianzheng

  • Subquery as a condition
  • The subquery form is a single row and multiple columns

5.4 Query the employee name, employee salary, department name, and department address whose employee number is 1006

5.4.1 The first way:

  • Subquery as a table
  • The subquery form is multiple rows and multiple columns

The second way:

Emp table and dept table do inner join query:

Guess you like

Origin blog.csdn.net/weixin_44679832/article/details/105281515