Multi-table query - "MySQL database"

Hello fellow CSDN users, today, the content of Xiaoyalan is multi-table query in MySQL database. This content is indeed a difficult point. Now, let us enter the world of multi-table query! ! !


Multi-table join triggered by one case

Multi-table query classification explanation

SQL99 syntax to implement multi-table query

Use of UNION

7 implementations of SQL JOINS

New features of SQL99 syntax

Chapter summary

Appendix: What are the commonly used SQL standards?


Multi-table join triggered by one case

Case description

Get data from multiple tables:

 

#Case: Query the employee’s name and department name

SELECT last_name, department_name

FROM employees, departments;

 

search result:

+-----------+----------------------+
| last_name | department_name     |
+-----------+----------------------+
| King     | Administration       |
| King     | Marketing           |
| King     | Purchasing           |
| King     | Human Resources     |
| King     | Shipping             |
| King     | IT                   |
| King     | Public Relations     |
| King     | Sales               |
| King     | Executive           |
| King     | Finance             |
| King     | Accounting           |
| King     | Treasury             |
...
| Gietz     | IT Support           |
| Gietz     | NOC                 |
| Gietz     | IT Helpdesk         |
| Gietz     | Government Sales     |
| Gietz     | Retail Sales         |
| Gietz     | Recruiting           |
| Gietz     | Payroll             |
+-----------+----------------------+

2889 rows in set (0.01 sec)

 Analyze the error situation:

SELECT COUNT(employee_id) FROM employees;

#Output 107 lines

SELECT COUNT(department_id)FROM departments;

#Output 27 lines

SELECT 107*27 FROM dual;

 We call the problem that occurs in the above multi-table query: Cartesian product error.

Understanding Cartesian Product (or Cross-Connection)

Cartesian product is a mathematical operation. Suppose I have two sets X and Y, then the Cartesian product of X and Y is all possible combinations of X and Y, that is, all possible combinations of the first object from X and the second object from Y. The number of combinations is the product of the number of elements in the two sets.

In SQL92, the Cartesian product is also called a cross join, which is CROSS JOIN in English. In SQL99, CROSS JOIN is also used to represent cross connections. Its function is to join any table, even if the two tables are not related. In MySQL, Cartesian product will occur in the following situations:  

#Query employee name and department name

SELECT last_name,department_name FROM employees,departments;

SELECT last_name,department_name FROM employees CROSS JOIN departments;

SELECT last_name,department_name FROM employees INNER JOIN departments;

SELECT last_name,department_name FROM employees JOIN departments;

Case analysis and problem solving

Cartesian product errors occur under the following conditions:

  • Omit join conditions (or association conditions) for multiple tables
  • The join condition (or association condition) is invalid
  • All rows in all tables are connected to each other

In order to avoid Cartesian product, you can add valid join conditions in WHERE.

After adding the connection conditions, the query syntax is:

SELECT table1.column, table2.column

FROM table1, table2

WHERE table1.column1 = table2.column2; #Connection conditions

Write the join condition in the WHERE clause.

 Correct way to write:

#Case: Query the employee’s name and department name

SELECT last_name, department_name

FROM employees, departments

WHERE employees.department_id = departments.department_id;

 When there are identical columns in a table, prefix the column name with the table name.


Multi-table query classification explanation

Category 1: Equivalent connection vs non-equivalent connection

Equijoin

SELECT employees.employee_id, employees.last_name,       employees.department_id, departments.department_id,       departments.location_id

FROM   employees, departments

WHERE employees.department_id = departments.department_id;

 

Extension 1: Multiple connection conditions and AND operator

 

Extension 2: Distinguish duplicate column names

When there are identical columns in multiple tables, the table name must be prefixed before the column name.

Columns with the same column name in different tables can be distinguished by the table name.

SELECT employees.last_name, departments.department_name,employees.department_id

FROM employees, departments

WHERE employees.department_id = departments.department_id;

Extension 3: Table alias

Using aliases can simplify queries.

Using the table name prefix before the column name can improve query efficiency.

SELECT e.employee_id, e.last_name, e.department_id,       d.department_id, d.location_id

FROM   employees e , departments d

WHERE e.department_id = d.department_id;

It should be noted that if we use the alias of the table, we can only use the alias in the query field and filter condition, and we cannot use the original table name, otherwise an error will be reported.  

Alibaba development specifications:

[Mandatory] For queries and changes to table records in the database, as long as multiple tables are involved, the table alias (or table name) needs to be qualified before the column name.

Note: When querying records, updating records, or deleting records in multiple tables, if the operation column does not have an alias (or table name) that qualifies the table, and the operation column exists in multiple tables, an exception will be thrown.

正例 :select t1.name from table_first as t1 , table_second as t2 where t1.id=t2.id;

Counter example: In a certain business, since the multi-table association query statement does not have the restriction of adding table aliases (or table names), after two years of normal operation, a field with the same name was recently added to a table and database changes were made in the pre-release environment. Finally, a 1052 exception occurred in the online query statement: Column 'name' in field list is ambiguous.

Extension 4: Connect multiple tables

Summary: To connect n tables, at least n-1 connection conditions are required. For example, to join three tables, at least two join conditions are required.  

non-equijoin

SELECT e.last_name, e.salary, j.grade_level

FROM   employees e, job_grades j

WHERE e.salary BETWEEN j.lowest_sal AND j.highest_sal;

 

Category 2: Self-connection vs. non-self-connection

When table1 and table2 are essentially the same table, they are just virtualized into two tables using aliases to represent different meanings. Then the two tables perform inner joins, outer joins and other queries.

Title: Query the employees table and return "Xxx works for Xxx"

SELECT CONCAT(worker.last_name ,' works for '       , manager.last_name)

FROM   employees worker, employees manager

WHERE worker.manager_id = manager.employee_id ;

 

Category 3: Inner join vs outer join

 In addition to querying records that meet the conditions, outer joins can also query records that do not meet the conditions on one side.

Inner join: Merges rows from more than two tables with the same column. The result set does not contain rows from one table that do not match another table.

Outer join: During the connection process of two tables, in addition to returning rows that meet the join conditions, it also returns rows in the left (or right) table that do not meet the conditions. This type of join is called a left (or right) outer join. When there are no matching rows, the corresponding column in the result table is NULL.

If it is a left outer join, the table on the left in the join condition is also called the master table, and the table on the right is called the slave table.

If it is a right outer join, the table on the right in the join condition is also called the master table, and the table on the left is called the slave table.

SQL92: Use (+) to create a connection

In SQL92, (+) is used to represent the location of the secondary table. That is, in a left or right outer join, (+) indicates which is the slave table.

Oracle has good support for SQL92, but MySQL does not support SQL92 outer joins.

#Left outer join

SELECT last_name,department_name

FROM employees ,departments

WHERE employees.department_id = departments.department_id(+);

#right outer join

SELECT last_name,department_name

FROM employees ,departments

WHERE employees.department_id(+) = departments.department_id;

And in SQL92, there are only left outer joins and right outer joins, not full (or full) outer joins.


SQL99 syntax to implement multi-table query

basic grammar

Syntax structure for creating joins using JOIN...ON clause:

SELECT table1.column, table2.column,table3.column

FROM table1    

        JOIN table2 ON connection conditions between table1 and table2

               JOIN table3 ON connection conditions between table2 and table3

Its nested logic is similar to the FOR loop we use:

for t1 in table1:    

        for t2 in table2:      

                if condition1:          

                        for t3 in table3:              

                                if condition2:                

                                        output t1 + t2 + t3  

The nested structure adopted by SQL99 is very refreshing, more hierarchical, and more readable. Even if more tables are connected, they are clearly visible. If you use SQL92, readability will be greatly reduced.

Syntax description:

  • You can use the ON clause to specify additional join conditions.
  • This join condition is separate from other conditions.
  • The ON clause makes the statement more readable.
  • The keywords JOIN, INNER JOIN, and CROSS JOIN have the same meaning, and they all represent inner joins.

Implementation of INNER JOIN

grammar:

SELECT field list

FROM A table INNER JOIN B table

ON related conditions

WHERE and other clauses;

Question 1:

SELECT e.employee_id, e.last_name, e.department_id, 
       d.department_id, d.location_id
FROM   employees e JOIN departments d
ON     (e.department_id = d.department_id);

Question 2:

SELECT employee_id, city, department_name
FROM   employees e 
JOIN   departments d
ON     d.department_id = e.department_id 
JOIN   locations l
ON     d.location_id = l.location_id;

Implementation of OUTER JOIN

LEFT OUTER JOIN

grammar:

#The query result is A

SELECT field list

FROM A table LEFT JOIN B table

ON related conditions

WHERE and other clauses;

 Example:

SELECT e.last_name, e.department_id, d.department_name
FROM   employees e
LEFT OUTER JOIN departments d
ON   (e.department_id = d.department_id) ;

RIGHT OUTER JOIN

 grammar:

#The query result is B

SELECT field list

FROM A table RIGHT JOIN B table

ON related conditions

WHERE and other clauses;

Example:

SELECT e.last_name, e.department_id, d.department_name
FROM   employees e
RIGHT OUTER JOIN departments d
ON   (e.department_id = d.department_id) ;

 

It should be noted that LEFT JOIN and RIGHT JOIN only exist in SQL99 and later standards, but do not exist in SQL92 and can only be expressed by (+).

FULL OUTER JOIN

  • The result of a full outer join = matching data in the left and right tables + no matching data in the left table + no matching data in the right table.
  • SQL99 supports full external connections. Use FULL JOIN or FULL OUTER JOIN to achieve this.
  • It should be noted that MySQL does not support FULL JOIN, but you can use LEFT JOIN UNION RIGHT join instead.

Use of UNION

Merging query results Using the UNION keyword, you can give multiple SELECT statements and combine their results into a single result set. When merging, the number of columns and data types corresponding to the two tables must be the same and correspond to each other. Each SELECT statement is separated by the UNION or UNION ALL keyword.

Syntax format:

SELECT column,... FROM table1

UNION [ALL]

SELECT column,... FROM table2

UNION operator

 The UNION operator returns the union of the result sets of two queries, removing duplicate records.

UNION ALL operator

The UNION ALL operator returns the union of the result sets of two queries. Duplicate parts of the two result sets are not deduplicated.

Note: The UNION ALL statement requires fewer resources than the UNION statement. If it is clear that there is no duplicate data in the resultant data after merging the data, or there is no need to remove duplicate data, try to use the UNION ALL statement to improve the efficiency of data query.  

Example: Query employee information whose department number is >90 or whose email address contains a

#方式1
SELECT * FROM employees WHERE email LIKE '%a%' OR department_id>90;
#方式2
SELECT * FROM employees  WHERE email LIKE '%a%'
UNION
SELECT * FROM employees  WHERE department_id>90;

Example: Query the information of male Chinese users and the user information of middle-aged male users in the United States

SELECT id,cname FROM t_chinamale WHERE csex='男'
UNION ALL
SELECT id,tname FROM t_usmale WHERE tGender='male';

7 implementations of SQL JOINS

Code

# Middle picture: Inner join A ∩ B

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d ` ;

# Upper left picture: left outer join

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e L E F T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d ` ;

# Upper right picture: Right outer join

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e R I G H T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d ` ;

# Middle left picture: A - A ∩ B

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e L E F T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d `

W H E R E d . ` d e p a r t m e n t _ i d ` I S N U L L

# Middle right picture: B - A ∩ B

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e R I G H T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d `

W H E R E e . ` d e p a r t m e n t _ i d ` I S N U L L

# Bottom left picture: full external connection

# Middle left picture + Upper right picture A ∪ B

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e L E F T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d `

W H E R E d . ` d e p a r t m e n t _ i d ` I S N U L L

UNIONALL # No deduplication operation, high efficiency

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e R I G H T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d ` ;

# Bottom right picture

# Left middle picture + Right middle picture A ∪ B - A ∩ B or ( A - A ∩ B ) ∪ ( B - A ∩ B )

S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e L E F T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d `

W H E R E d . ` d e p a r t m e n t _ i d ` I S N U L L

U N I O N A L L S E L E C T e m p l o y e e _ i d , l a s t _ n a m e , d e p a r t m e n t _ n a m e

F R O M e m p l o y e e s e R I G H T J O I N d e p a r t m e n t s d

O N e . ` d e p a r t m e n t _ i d ` = d . ` d e p a r t m e n t _ i d `

W H E R E e . ` d e p a r t m e n t _ i d ` I S N U L L

Syntax format summary

Middle left picture

# Implement A - A ∩ B

select field list

f r o m A 表 l e f t j o i n B 表

on associated conditions

other clauses such as where isnulland and other clauses related to the field from the table;

Middle right picture

# Implement B - A ∩ B

select field list

f r o m A 表 r i g h t j o i n B 表

on associated conditions

other clauses such as where isnulland and other clauses related to the field from the table;

Lower left picture

# The query result is A ∪ B # Use the left outer A and union the right outer B

select field list

f r o m A 表 l e f t j o i n B 表

on associated conditions

where and other clauses

u n i o n

select field list

f r o m A 表 r i g h t j o i n B 表

on associated conditions

other clauses such as where ;

lower right picture

#Realize A∪B - A∩B or (A - A∩B) ∪ (B - A∩B)

#Use left outer (A - A∩B) union right outer (B - A∩B)

select field list

from table A left join table B

on associated conditions

where related fields from the table are null and other clauses

union select field list

from table A right join table B

on associated conditions

where related fields from the table are null and other clauses


New features of SQL99 syntax

 natural connection

SQL99 provides some special syntax based on SQL92, such as NATURAL JOIN to represent natural joins. We can understand natural joins as equal joins in SQL92. It will help you automatically query all the same fields in the two connected tables, and then perform an equal value join.

In the SQL92 standard:

SELECT employee_id,last_name,department_name
FROM employees e JOIN departments d
ON e.`department_id` = d.`department_id`
AND e.`manager_id` = d.`manager_id`;

In SQL99 you can write:

SELECT employee_id,last_name,department_name
FROM employees e NATURAL JOIN departments d;

USING connection

When we connect, SQL99 also supports using USING to specify the same-name field in the data table for equi-join. But it can only be used with JOIN. for example:

SELECT employee_id,last_name,department_name

FROM employees e JOIN departments d

USING (department_id);

You can see that the difference from NATURAL JOIN is that USING specifies the same specific field name. You need to fill in the field with the same name to be specified in the brackets () of USING. Using JOIN...USING at the same time can simplify the equivalent connection of JOIN ON. It is the same as the following SQL query result:

SELECT employee_id,last_name,department_name
FROM employees e ,departments d
WHERE e.department_id = d.department_id;

Chapter summary

Table connection constraints can be in three ways: WHERE, ON, USING

  • WHERE: Applies to all related queries
  • ON: Can only be used with JOIN, and can only write associated conditions. Although related conditions can be written together with other conditions in WHERE, it is more readable to write them separately.
  • USING: can only be used with JOIN, and requires that the two related fields have the same names in the related table, and can only mean that the values ​​of the related fields are equal.
#关联条件
#把关联条件写在where后面
SELECT last_name,department_name 
FROM employees,departments 
WHERE employees.department_id = departments.department_id;

#把关联条件写在on后面,只能和JOIN一起使用
SELECT last_name,department_name 
FROM employees INNER JOIN departments 
ON employees.department_id = departments.department_id;

SELECT last_name,department_name 
FROM employees CROSS JOIN departments 
ON employees.department_id = departments.department_id;

SELECT last_name,department_name  
FROM employees JOIN departments 
ON employees.department_id = departments.department_id;

#把关联字段写在using()中,只能和JOIN一起使用
#而且两个表中的关联字段必须名称相同,而且只能表示=
#查询员工姓名与基本工资
SELECT last_name,job_title
FROM employees INNER JOIN jobs USING(job_id);

#n张表关联,需要n-1个关联条件
#查询员工姓名,基本工资,部门名称
SELECT last_name,job_title,department_name FROM employees,departments,jobs 
WHERE employees.department_id = departments.department_id 
AND employees.job_id = jobs.job_id;

SELECT last_name,job_title,department_name 
FROM employees INNER JOIN departments INNER JOIN jobs 
ON employees.department_id = departments.department_id 
AND employees.job_id = jobs.job_id;

 Notice:

We need to control the number of join tables. Multi-table joins are equivalent to nested for loops, which consume a lot of resources and seriously degrade SQL query performance. Therefore, do not join unnecessary tables. In many DBMS, there is also a limit on the maximum join table.

[Mandatory] Joining more than three tables is prohibited. The data types of the fields that need to be joined must be absolutely consistent; when performing multi-table related queries, it is ensured that the related fields need to have indexes.

Note: Even when joining two tables, you must pay attention to table indexes and SQL performance.


Appendix: What are the commonly used SQL standards?

When we start to officially talk about the types of connection tables, we first need to know that there are different versions of SQL standard specifications, because the table connection operations under different specifications are different.

SQL has two main standards, namely SQL92 and SQL99. 92 and 99 represent the time when the standard was proposed, and SQL92 was the standard specification proposed in 1992. Of course, in addition to SQL92 and SQL99, there are other standards such as SQL-86, SQL-89, SQL:2003, SQL:2008, SQL:2011 and SQL:2016.

With so many standards, which one should we learn? In fact, the most important SQL standards are SQL92 and SQL99 . Generally speaking, the form of SQL92 is simpler, but the SQL statements written will be longer and less readable. Compared with SQL92, SQL99 has more complex syntax, but is more readable. We can also see from the number of pages published by these two standards that the SQL92 standard has 500 pages, while the SQL99 standard exceeds 1,000 pages. In fact, since SQL99, few people can master everything because there are too many. Just like when we use Windows, Linux and Office, few people can master all the contents. We only need to master some core functions to meet the needs of daily work.

SQL92 and SQL99 are classic SQL standards, also called SQL-2 and SQL-3 standards respectively. It was after the release of these two standards that SQL became more and more influential, even beyond the database field. Nowadays, SQL is not only the mainstream language in the database field, but also the mainstream language for information processing in the information field. The use of SQL language can be seen in graphic retrieval, image retrieval and voice retrieval.


Okay, that’s all Xiao Yalan’s content for today, keep up the good work! ! !

Guess you like

Origin blog.csdn.net/weixin_74957752/article/details/132945763
Recommended