[Advanced Java] Detailed explanation of MySQL multi-table query

Insert image description here

MySQL is a powerful relational database management system, and multi-table query is one of the important parts of database operations. Multi-table queries allow you to retrieve and manipulate data from multiple tables to meet complex data needs. This article will introduce the basic concepts, syntax and examples of MySQL multi-table query, as well as some common multi-table query scenarios.

What is a multi-table query?

In a relational database, data is usually spread across multiple tables rather than stored in a single table. Multi-table queries are operations that retrieve data from more than one table and combine them to meet specific needs. With multi-table queries, you can do the following:

  • Retrieve data associated with multiple tables.
  • Create relationships between multiple tables to facilitate data analysis.
  • Aggregate and calculate data from multiple tables.
  • Update and delete data in multiple tables.

Multi-table queries typically involve joining different tables together using a JOIN clause to create a result set that contains the required data.

Basic syntax of multi-table query

In MySQL, use JOINthe clause to perform multi-table queries. JOINclause is used to combine rows from two or more tables to create a result set that contains data from those tables. The basic JOINclause syntax is as follows:

SELECT 列名
FROM1
JOIN2
ON1.=2.;

in:

  • SELECTstatement specifies the columns to retrieve.
  • 表1and 表2are the tables to be joined.
  • ONclause specifies join conditions, i.e. which columns should match to create a join.

Here is a simple example showing how to retrieve data from two tables:

SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

In this example, we retrieve data from two tables named ordersand customersand customer_idjoin them together based on the column.

Different types of JOIN

MySQL supports different types of JOIN operations to meet different data requirements. The following are some common JOIN types:

  1. INNER JOIN : INNER JOIN returns matching rows from two tables, and only matching rows. If there are no matching rows in the two tables, no results are returned.

  2. LEFT JOIN (or LEFT OUTER JOIN) : LEFT JOIN returns all rows in the left table and rows in the right table that match the left table. If there is no matching row in the right table, a NULL value is returned.

  3. RIGHT JOIN (or RIGHT OUTER JOIN) : RIGHT JOIN is the opposite of LEFT JOIN, it returns all the rows in the right table and the rows in the left table that match the right table. If there is no matching row in the left table, a NULL value is returned.

  4. FULL JOIN (or FULL OUTER JOIN) : FULL JOIN returns all rows in both tables, or NULL value if there are no matching rows.

  5. CROSS JOIN : CROSS JOIN returns the Cartesian product of two tables, that is, each row in the left table is combined with each row in the right table.

Example: Common scenarios of multi-table queries

Scenario 1: Retrieve order and customer information

Suppose you have two tables, one containing order information and the other containing customer information. You want to retrieve each order and the customer information associated with it. This is a typical INNER JOIN example:

SELECT orders.order_id, customers.customer_name
FROM orders
JOIN customers
ON orders.customer_id = customers.customer_id;

Scenario 2: Find customers without orders

Sometimes you may need to find customers who have not placed an order. These customers can be found using LEFT JOIN:

SELECT customers.customer_id, customers.customer_name
FROM customers
LEFT JOIN orders
ON customers.customer_id = orders.customer_id
WHERE orders.customer_id IS NULL;

In this query, we use a LEFT JOIN to get all customer information, and then use WHEREthe clause to filter out those orderscustomers who do not have matching orders in the table.

Scenario 3: Calculate the average price of each category

Suppose you have two tables, one containing product information and the other containing product category information. You want to calculate the average price for each product category. This can be achieved by using GROUP BY and aggregate functions:

SELECT categories.category_name, AVG(products.price) AS avg_price
FROM categories
JOIN products
ON categories.category_id = products.category_id
GROUP BY categories.category_name;

In this query, we first join categoriesthe tables productstogether and then GROUP BYgroup by category name using the clause. Finally, we use AVGthe function to calculate the average price for each category.

Scenario 4: Update data in multiple tables

Sometimes you need to update data in multiple tables. For example, you might need to update information in the orders table and products table to reflect price changes. UPDATEThis can be done using multiple statements, each UPDATEupdating one table.

-- 更新订单表中的价格
UPDATE orders
JOIN products
ON orders.product_id = products.product_id
SET orders.price = products.price;

-- 更新产品表中的价格
UPDATE products
JOIN orders
ON products.product_id = orders.product_id
SET products.price = orders.price;

In this example, we first join the orders and products tables together, and then use two UPDATEstatements to update the prices in the orders and products tables respectively.

Summarize

MySQL multi-table query is an important tool for handling complex data requirements in relational databases. By understanding the different types of JOIN operations and how to write multi-table query statements, you can perform a variety of complex data operations, including data retrieval, aggregation, updates, and deletions. When doing multi-table queries, make sure you understand the relationship between each table and choose the appropriate JOIN type to meet your needs. I hope this article can help you better understand and apply MySQL multi-table queries.

Author information

Author: Fanyi
CSDN: https://techfanyi.blog.csdn.net
Nuggets: https://juejin.cn/user/4154386571867191

Guess you like

Origin blog.csdn.net/qq_21484461/article/details/133419366