English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

SQL Tabellenverknüpfung (Join)

In this tutorial, you will learn how to join two tables to get combined data.

SQL Join Basics

So far, all the queries you have seen have been focused on one table. However, 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 tables based on common fields (Foreign key) Join different tables to create a new view of data.

To understand this more easily, let's look at the followingemployeesunddepartmentsTable. Here, the dept id column of the employees table is a foreign key in the departments table. Therefore, these two tables can be connected 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 of the columns used for joining should match, not necessarily the column names.

Join type

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:

Inner join (Inner join)

The join only returns rows that have matching items in both joining tables. For example, you can join the employees anddepartmentsThe tables are connected to create a result set that displays the department names of each employee. In an inner join, employees without department information are not included in the result set, and departments without employees are also not included in the result set.

In the next chapter, we will learn aboutInternally connected更多信息。

Für mehr Informationen.

Externe Verknüpfung (Outer join)Externe Verknüpfungen sind eine Erweiterung der inneren Verknüpfungen. Selbst wenn externe Verknüpfungen keine entsprechenden Zeilen in der Verknüpfungstabelle haben, geben sie diese Zeilen zurück. Externe Verknüpfungen gibt es drei Arten: linker Verknüpfung () linker Verknüpfung () rechter Verknüpfung () und vollständige Verknüpfung (vollständige Verknüpfung))

Wir werden diese Varianten der externen Verknüpfung im folgenden Kapitel im Detail besprechen.

Kreuzverbindung (Cross join)

Die Kreuzverbindung ist eine Verknüpfung ohne Verknüpfungskonditionen. Jede Zeile einer Tabelle wird mit jeder Zeile der anderen Tabelle kombiniert. Dieses Typ der Ergebnismenge wird als kartesisches Produkt oder Kreuzprodukt bezeichnet. Zum Beispiel,employeesunddepartmentsDie Kreuzverbindung zwischen Tabellen erzeugt ein Ergebnisset, in dem jeder mögliche Mitarbeiter/Die Kombinationen der Abteilungen haben jede Zeile.

In den folgenden Kapiteln werden wir lernen überKreuzverbindungFür mehr Informationen.