[Java Advanced] MySQL multi-table query: detailed explanation of inner joins

Insert image description here

MySQL is a powerful relational database management system that allows you to perform complex query operations across multiple tables. This article will focus on an important type of multi-table query in MySQL: INNER JOIN. Inner joins are used to retrieve rows that satisfy the association between two or more tables. They help you combine data from multiple tables to better understand and analyze the data.

What is an inner join?

Inner joins, also known as EQUIJOINs, are a basic table join type. Inner joins retrieve matching rows between two tables that meet the join criteria and merge them into a single result set. In an inner join, only those rows that match under the join conditions are included in the result set.

Inner join is the most commonly used connection type, which helps us obtain related data from multiple tables. Here is a simple inner join syntax:

SELECT 列名
FROM1
INNER JOIN2
ON1.列名 =2.列名;

In this syntax, we use INNER JOINthe keyword to specify the inner join, and then ONspecify the join condition in the clause, which is usually a column shared between the two tables.

Inner join example

To better understand inner joins, let’s demonstrate its usage with some examples.

Example 1: Join two tables

Suppose we have two tables: a table containing customer information customersand a table containing order information orders. We want to get order information for each customer. This can be achieved with an inner join:

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

In this query, we join customerstable and orderstable together and ONspecify the join condition in clause customers.customer_id = orders.customer_idi.e. This will return order information for each customer, including customer name and order date.

Example 2: Join multiple tables

Sometimes, we need to join multiple tables to get more information. Suppose we have an additional table productscontaining information about the products in the order. We can join the three tables together using an inner join:

SELECT customers.customer_name, orders.order_date, products.product_name
FROM customers
INNER JOIN orders
ON customers.customer_id = orders.customer_id
INNER JOIN products
ON orders.product_id = products.product_id;

In this query, we first join customerstable and orderstable, and then join orderstable and productstable. This will return each customer's order information and the names of the products included in the order.

Example 3: Concatenate multiple conditions

Sometimes, we need to join tables based on multiple conditions. For example, suppose we have a table employeesthat contains information about employees, and a table departmentsthat contains information about departments. We want to get the name and address of each employee's department. We can use multiple conditions to join these two tables:

SELECT employees.employee_name, departments.department_name, departments.department_address
FROM employees
INNER JOIN departments
ON employees.department_id = departments.department_id
AND employees.location_id = departments.location_id;

In this query, we use two conditions to join employeestable and departmentstable, namely employees.department_id = departments.department_idand employees.location_id = departments.location_id. This will return the name and address of each employee's department.

The difference between inner join and outer join

In an inner join, only rows that meet the join conditions are included in the result set, and rows that do not meet the conditions are excluded. This means that the result set returned by the inner join does not contain unmatched rows. Unlike inner joins, outer joins (LEFT JOIN, RIGHT JOIN, FULL JOIN) return unmatched rows and fill them with NULL values.

Summarize

Inner join is one of the most commonly used connection types in MySQL, which is used to retrieve matching rows between two or more tables that meet the join conditions. By properly using inner joins, you can obtain related data from multiple tables and conduct more complex queries and data analysis. In this article, we've taken a deep dive into the basic concepts and syntax of inner joins and provided some practical examples to help you better understand its usage.

When performing multi-table queries, in addition to inner joins, you can also explore other types of joins, such as left joins, right joins, and full joins, to meet different needs. In addition, you can use subqueries, aggregate functions, and other SQL features to further expand query capabilities.

Through continuous learning and practice, you will be more proficient in using MySQL for multi-table queries to solve various complex data analysis and reporting needs. I hope this article will be helpful to you when learning and using MySQL. If you have any questions or need further assistance, please feel free to ask. I wish you success in your journey of database querying and data analysis!

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/133419404