Do you know these connection queries in the multi-table connection query of mainstream databases?

Article directory

 

content

Article directory

foreword

4.3. Multi-table query

Equijoin

inner join

outer join

Left Outer Join and Right Outer Join

cross connect

Merge result sets

unequal connection



foreword

Hello everyone, I am the color of ice three points. Personal homepage: blog of ice three colors

This article talks about equijoins, inner joins, outer joins, merged result sets, and unequal links.

Friends passing by, please like and follow before walking. Welcome to the comment area to communicate. It is never too late to start working hard, so it is better to start with this article!

Let's grow together! refill


4.3. Multi-table query

Multi-table query refers to querying the required data from two or more tables in one query statement.

Multi-table query is to query data through the association of common columns between each table (primary key foreign key, master-slave table query), which is the main feature of relational database query.

MySQL provides the following join queries between multiple tables

Equijoin

Equi join is a join query that uses the equality comparator "=" to specify join conditions. This join query is mainly used to retrieve related data between master and slave tables.

SELECT [alias of table1.]columnname1,....[alias of table2.]columnname1,..., of table2

FROM table 1 [alias 1], table 2 [alias 2]

WHERE table name or alias 1.column= table name or alias 2.column;

Note: 1. Equi-join requires that the values ​​of a column in the two tables are completely equal before joining.

2. The tables to be connected should be written after FROM first, and the table names should be separated by commas.

3. The conditions of the join are placed in the WHERE clause. Whether to use the table name or alias after WHERE depends on whether the alias is set after FROM. To join n tables requires at least n-1 join conditions.

4. If the columns of the query are unique among the tables participating in the join, the table name prefix can be omitted. If there are the same column names in multiple tables, add the table name in front of these columns to distinguish them

Example: Query the name and location of the department to which each employee belongs (because the deptno column in the employee table is a foreign key, and its value comes from the deptno column of the department table, so the deptno column can be used to achieve the equivalent connection between the employee table and the department table )

SELECT emp.empno,emp.ename,emp.deptno,dept.dname,dept.loc

FROM employee emp,department dept

WHERE emp.deptno=dept.deptno;

-- Connect the two tables and their corresponding columns by using the equality comparator "=" to join the query.

Example: Query the departments and specific locations of employees whose salary is 2,000 yuan or more.

SELECT e.empno,e.ename,e.deptno,d.dname,d.loc

FROM employee e,department d

WHERE e.deptno=d.deptno AND e.sal>=2000

ORDER BY e.empno ASC;

inner join

Inner join has the same function as equijoin, and both are used to return records that satisfy the join conditions. Among them: INNER JOIN represents an inner join, and ON is used to specify the join conditions between the two tables.

SELECT [alias of table1.]columnname1,....[alias of table2.]columnname1,..., of table2

FROM table 1 [alias 1] [INNER] JOIN table 2 [alias 2]

ON table1.column_name = table2.column_name;

Example: Use inner join to query the name, position, direct manager name and department name of employees whose positions are Clerk and Analyst, and the displayed results are sorted in ascending order by employee number.

SELECT e.ename 'employee name', e.job 'position' , m.ename 'manager name', d.dname 'department name' FROM employee e

JOIN employee m ON e.mgr=m.empno

JOIN department d ON e.deptno=d.deptno

WHERE e.job IN('CLERK','ANALYST')

ORDER BY e.empno;

outer join

An outer join is an extension of an inner join, which returns not only all records that satisfy the join conditions, but also records that do not. There are three types of outer joins: left outer join, right outer join, and cross join.

Left Outer Join and Right Outer Join

Not only will return all records in the join table that satisfy the join condition, but also other rows from the left table of the join operator that do not satisfy the join condition

SELECT [Alias ​​for table 1.] Column name 1,....[Alias ​​for table 2.] Column name 1,..., FROM table 2 for table 1 [alias 1]

LEFT [OUTER] JOIN table2 ON table1.columnname=table2.columnname;

Similar to the left outer join function, it not only returns all records that satisfy the join condition, but also returns other rows of the right table of the join operator that do not satisfy the join condition.

SELECT [Alias ​​for table 1.] Column name 1,....[Alias ​​for table 2.] Column name 1,..., FROM table 2 for table 1 [alias 1]

RIGHT [OUTER] JOIN table2 ON table1.columnname=table2.columnname;

Example: When viewing the department table, you will find that there is a department No. 40, and there is no employee in the employee table belonging to this department. Therefore, when using equi-join to query the information of employees and their departments, the information of this department is not displayed. . At this time, if it is necessary to display the information of all departments (including departments without employees) at the same time.

SELECT e.empno,e.ename,d.deptno,d.dname,d.loc FROM department d

LEFT JOIN employee e ON d.deptno=e.deptno

ORDER BY d.deptno DESC;

Example: Query all department information (including those without employees) and their corresponding employee information.

SELECT e.empno,e.ename,d.deptno,d.dname,d.loc FROM employee e

RIGHT JOIN department d ON e.deptno=d.deptno

ORDER BY d.deptno DESC;

cross connect

is the Cartesian product of the resulting tables without the where clause. When two tables are cross-connected, the result set size is the product of the two rows. This method is rarely used in practice.

Example: Display the Cartesian product of employee table and department table, a total of 10*4=40 records.

SELECT e.ename,e.deptno,d.deptno,d.dname,d.loc FROM employee e

CROSS JOIN department d;

 

Note: 1. In theory, there is no upper limit on the number of tables that can be connected using the select statement. However, if more than 10 tables are connected in a select statement, the database may not be optimally designed, and the execution plan of the MySQL engine will become very cumbersome.

2. For join queries of more than 3 relational tables, generally follow the following rules: At least n-1 join conditions are required to join n tables to avoid the appearance of Cartesian products. In order to narrow the result set, it is permissible to use more than n-1 join conditions or to use other conditions.

Merge result sets

The union operator can combine the results of multiple select statements into a single result set.

When the data to be retrieved is in different result sets and cannot be obtained by a single query statement, union can be used to combine multiple result sets.

Combines the results of two or more queries into a single result set that contains all the rows from all the queries in the union query.

When merging two query result sets using union, the number and order of columns in all queries must be the same and the data types must be compatible.

Format

select_statement1

UNION[ALL]

select_statement2

Among them: select_statement: select statement. UNION: Specifies that multiple result sets are combined and returned as a single result set.

All: Combine all rows into the result, including duplicate rows. If not specified, duplicate rows will be automatically removed.

Example: Partial query result sets of tables are merged (create an emp table first, and then query)

CREATE TABLE emp AS SELECT empno,ename,job FROM employee;

SELECT empno,ename,job FROM emp WHERE ename like "%AR%"

UNION

SELECT empno,ename,job FROM employee WHERE ename like "%AM%";

unequal connection

Unequal join refers to the mutual association between two tables through the comparison operation of two related columns. Comparison operations generally use operators such as ">", "<", and "BETWEEN...AND".

Example: Query the employee's number, name, position, salary, and the level corresponding to the salary. The salary grades are stored in the SALGRADE (salary grade table).

First query the next two tables, as shown below (employee table and salgrade table, if you don't have one, create it yourself)

SELECT*from employee;

SELECT*from salgrade;

SELECT e.empno,e.ename,e.job,e.sal,s.grade

FROM employee e,salgrade s

WHERE e.sal BETWEEN s.losal AND s.hisal;

 

Guess you like

Origin blog.csdn.net/qq_46007633/article/details/124028483