Home Database Mysql Tutorial Detailed explanation of mysql's multi-table query

Detailed explanation of mysql's multi-table query

Aug 23, 2019 pm 03:48 PM
Multi-table query

First, in order to facilitate the explanation of the problem, create two tables emp (employee information) and dept (employee department information). The data is as follows:

Detailed explanation of mysqls multi-table queryIn the previous article, we shared the single Table query method, but in actual applications, the data we want to query is likely not in the same table, but comes from different tables. If a multi-table query does not add any conditions, the result obtained is called a Cartesian product. For example, find the employee name, employee salary, and department name.

Detailed explanation of mysqls multi-table query

It can be found that the result is like this, select the first record from the first table, combine it with all the records in the second table, and then Then take the second record from the first table and combine it with all the records in the second table. This result is meaningless. What we need is the record with emp.deptno = dept.deptno field.

Detailed explanation of mysqls multi-table query

Self-join

Self-join refers to joining the query in the same table

Display the superior of employee FORD Leader’s name

Detailed explanation of mysqls multi-table query

Subquery

Subquery refers to a select statement embedded in other sql statements, also called nesting Inquire.

Single row subquery: The query result of the subquery has only one row

Displays employees in the same department as SMITH

Detailed explanation of mysqls multi-table query

Multi-row subquery (in, all, any): Subquery that returns multiple records

Query the names, jobs, salaries, and departments of officials who have the same job as the 10 best departments number, but does not include No. 10’s own information

Detailed explanation of mysqls multi-table query

Displays the name, salary and department number of employees whose salary is higher than the salary of all employees with department number 30

Detailed explanation of mysqls multi-table query

Display the name, salary and department number of the employee whose salary is higher than the salary of any employee with department number 30

Detailed explanation of mysqls multi-table query

Multi-column subquery: The query returns a subquery statement of multiple columns of data

Query employees who have the same department and position as SMITH, excluding SMITH himself

Detailed explanation of mysqls multi-table query

Use a subquery in the from clause

Display employee information that is higher than the average salary of your department

Detailed explanation of mysqls multi-table queryFind The ename, job, sal of the person with the highest salary in each department

Detailed explanation of mysqls multi-table queryDisplay the information (department name, number, address) and number of personnel of each department

Detailed explanation of mysqls multi-table query

There are two methods that can be used here, but it has been verified that when there is a lot of data, the efficiency of the from subquery is higher than that of the multi-table query.

Delete duplicate records in the table

It is known that there is duplicate data in a table tt

Create an empty table tmp_tt. The structure of the empty table is the same as The structure of table tt is the same; create table tmp_tt like tt;

Distinct the tt table and import the data into the empty table; insert into tmp_tt select distinct * from tt;

Delete the original table ttdrop table tt;

Rename tmp_tt to ttalter table tmp_tt rename tt;

Merge query

In practical applications, in order to merge multiple selects To execute the results, you can use union and union all set operators

1. The union operator is used to obtain the union of two results, and then automatically remove duplicate rows to find people with a salary greater than 2500 and a position of Manger.

Detailed explanation of mysqls multi-table query

2. union all is similar to union, but will not automatically remove duplicates
For example: similar to or

Detailed explanation of mysqls multi-table query

Foreign key

Foreign key defines the relationship between the master table and the slave table. Foreign key constraints are mainly defined on the slave table. The master table must have a primary key or a unique key. When a foreign key is defined, the foreign key column data must exist in the main table column or be NULL.
For example: create a main table class, slave table stu

Detailed explanation of mysqls multi-table query

Detailed explanation of mysqls multi-table query

From the above figure, we can know that the primary key cannot be null. However, the foreign key can be null, and the data in the foreign key cannot exist but does not exist in the main table.

For more related questions, please visit the PHP Chinese website: mysql video tutorial

The above is the detailed content of Detailed explanation of mysql's multi-table query. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Multi-table related query skills in PHP Multi-table related query skills in PHP May 24, 2023 am 10:01 AM

Multi-table related query skills in PHP Related queries are an important part of database queries, especially when you need to display data in multiple related database tables. In PHP applications, multi-table related queries are often used when using databases such as MySQL. The meaning of multi-table association is to compare data in one table with data in another or multiple tables, and connect those rows that meet the requirements in the result. When performing multi-table correlation queries, you need to consider the relationship between tables and use appropriate correlation methods. The following introduces several types of

Master MyBatis multi-table queries: a practical guide to optimizing data processing efficiency Master MyBatis multi-table queries: a practical guide to optimizing data processing efficiency Feb 19, 2024 pm 06:36 PM

Comprehensively master MyBatis multi-table query: a practical guide to improve data processing efficiency Introduction: Today, in software development, data processing efficiency is an important consideration. For data processing involving multi-table queries, MyBatis is a powerful tool. This article will delve into how to fully master MyBatis multi-table queries and improve the efficiency of data processing. The article will demonstrate through specific code examples to help readers better understand and apply. 1. Configure the MyBatis environment. First, we need to configure My

MyBatis multi-table query optimization: methods and strategies to improve SQL performance MyBatis multi-table query optimization: methods and strategies to improve SQL performance Feb 18, 2024 pm 10:24 PM

In-depth analysis of MyBatis multi-table queries: tips and strategies for optimizing SQL performance Summary: MyBatis is a commonly used persistence layer framework that can help us operate the database more conveniently. In actual development, multi-table query is a very common requirement, but performing multi-table query in an inappropriate way may lead to performance degradation. This article will focus on how to use MyBatis for multi-table queries, and give some tips and strategies for optimizing SQL performance. Introduction MyBatis is a popular Java persistence layer framework,

Analyze common problems in MyBatis multi-table association queries: Solve doubts in data connection queries Analyze common problems in MyBatis multi-table association queries: Solve doubts in data connection queries Feb 18, 2024 am 10:41 AM

Analysis of common problems in MyBatis multi-table query: To solve the confusion in data correlation query, specific code examples are needed. Introduction: In database application development, correlation query between data tables is a very common requirement. For the MyBatis framework, multi-table query is a very important function. However, due to the flexibility and powerful dynamic SQL capabilities of MyBatis, sometimes developers may encounter some confusion when performing multi-table queries. This article will describe some common problems and provide specific code examples to solve them.

How to optimize multi-table queries in PHP and MySQL through indexes? How to optimize multi-table queries in PHP and MySQL through indexes? Oct 15, 2023 pm 05:40 PM

How to optimize multi-table queries in PHP and MySQL through indexes? When developing web applications, interactions with databases are often involved. Especially for relational databases, multi-table queries are very common operations. However, when the amount of data is too large and the query complexity increases, the performance of multi-table queries may be affected to a certain extent. In order to improve query efficiency, we can make adjustments by optimizing the index. An index is a data structure used in a database to improve query performance. It can speed up data search. in P

MySql multi-table query: How to perform efficient multi-table data query MySql multi-table query: How to perform efficient multi-table data query Jun 15, 2023 pm 02:37 PM

With the development of the Internet and the continuous expansion of application fields, the increase in data volume has become the norm, and efficient data query is particularly important. When using MySQL database, multi-table query is an extremely common data query method. Therefore, how to perform efficient multi-table data query has become a skill that MySQL database users must master. This article will introduce how to perform efficient multi-table data query from the following aspects: 1. Basic concepts and syntax of multi-table query; 2. Optimization skills of multi-table query; 3. Common problems of multi-table query

MySql views: How to efficiently query data from multiple tables MySql views: How to efficiently query data from multiple tables Jun 15, 2023 pm 10:06 PM

MySql is a widely used relational database management system that is used in applications of all sizes and types, especially in web applications, enterprise solutions, and data analysis tools. In these applications, it is often necessary to query data from multiple tables. Therefore, in order to improve query efficiency and reduce the complexity of writing a large number of SQL statements, MySql introduces the concept of views. A view is a virtual table that does not really exist in the database. Instead, it consists of one or more SELECT statements.

Multi-database and multi-table query optimization practice in PHP programming Multi-database and multi-table query optimization practice in PHP programming Jun 22, 2023 pm 02:48 PM

PHP is a commonly used Web programming language. Its wide application means that we often need to operate multiple databases and tables in PHP code. Multi-database and multi-table query operations are frequent and time-consuming, so optimizing query efficiency has become a challenge that must be faced in PHP programming. In this article, we will share some practical methods for multi-database and multi-table query optimization. 1. Use the correct query method. The choice of query method has an important impact on the performance of multi-database and multi-table queries. When performing multi-database and multi-table queries, there are three commonly used query methods: JOIN queryJ

See all articles