English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this tutorial, you will learn how to join two tables to get combined data.
So far, all the queries you have seen have been focused on a single table. But in real life, you often need to query two or more tables at once and bring a merged result set. This is technically called a join because it involves joining based on common fields between tables (Foreign key)Joining different tables to create a new view of data.
To make this easy to understand, let's look at the followingemployeesetdepartmentsTable. Here, the dept id column of the employees table is a foreign key in the departments table. Therefore, these two tables can be joined to get combined data.
+--------+--------------+------------+---------+ | emp_id | emp_name | hire_date | dept_id | +--------+--------------+------------+---------+ | 1 | Ethan Hunt | 2001-05-01 | 4 | | 2 | Tony Montana | 2002-07-15 | 1 | | 3 | Sarah Connor | 2005-10-18 | 5 | | 4 | Rick Deckard | 2007-01-03 | 3 | | 5 | Martin Blank | 2008-06-24 | NULL | +--------+--------------+------------+---------+ | +---------+------------------+ | dept_id | dept_name | +---------+------------------+ | 1 | Administration | | 2 | Customer Service | | 3 | Finance | | 4 | Human Resources | | 5 | Sales | +---------+------------------+ | |
Table: employees | Table: departments |
Note:To join tables, the data in the columns used for joining should match, not necessarily the column names.
When joining tables, the type of join created in the query affects the rows displayed in the result set. You can create the following types of joins:
The join only returns rows that have matching items in both join tables. For example, you can join the employees anddepartmentsTables are joined together to create a result set that displays the department name for each employee. In an inner join, employees without department information are not included in the result set, and departments without employees will also not be included in the result set.
In the next chapter, we will learn aboutInternally connectedPour plus d'informations.
Les jointures externes sont une extension des jointures internes. Même si les jointures externes n'ont pas de lignes correspondantes dans la table de jointure, elles renverront ces lignes. Il y a trois types de jointures externes : jointure gauche (left join)right join) et jointure complète (full join)
Nous en apprendrons plus sur ces variantes de jointure externe dans les chapitres suivants.
Le croissement est une jointure sans condition de jointure. Chaque ligne d'une table est fusionnée avec chaque ligne de l'autre table. Ce type de jeu de résultats est appelé produit cartésien ou croisé. Par exemple,employeesetdepartmentsLe croissement des tables produit un ensemble de résultats, où chaque employé possible/Les combinaisons de départements ont une ligne.
Dans les prochaines sections, nous allons étudier les informations surCroisementPour plus d'informations.