返回介绍

Joining tables

发布于 2025-02-22 22:20:15 字数 5630 浏览 0 评论 0 收藏 0

In this part of the SQLite tutorial, we will join tables in SQLite.

The real power and benefits from relational databases come from joining tables. The SQL JOIN clause combines records from two or more tables in a database. There are basically two types of joins: INNER and OUTER .

In this part of the tutorial, we will work with Customers and Reservations tables.

sqlite> SELECT * FROM Customers;
CustomerId  Name     
----------  -----------
1       Paul Novak 
2       Terry Neils
3       Jack Fonda 
4       Tom Willis 

These are values from the Customers table.

sqlite> SELECT * FROM Reservations;
Id  CustomerId  Day     
--  ----------  ----------
1   1       2009-22-11
2   2       2009-28-11
3   2       2009-29-11
4   1       2009-29-11
5   3       2009-02-12

These are values from the Reservations table.

Inner joins

The inner join is the most common type of join. It is the default join also. The inner join selects only those records from database tables that have matching values. We have three types of INNER JOINS : INNER JOIN , NATURAL INNER JOIN and CROSS INNER JOIN . The INNER keyword can be omitted.

INNER JOIN

sqlite> SELECT Name, Day FROM Customers AS C JOIN Reservations
   ...> AS R ON C.CustomerId=R.CustomerId;
Name     Day    
-----------  -----------
Paul Novak   2009-22-11 
Terry Neils  2009-28-11 
Terry Neils  2009-29-11 
Paul Novak   2009-29-11 
Jack Fonda   2009-02-12 

In this SELECT statement, we have selected all customers that have made some reservations. Note that we have omitted the INNER keyword.

The statement is equivalent to the following one:

sqlite> SELECT Name, Day FROM Customers, Reservations
   ...> WHERE Customers.CustomerId = Reservations.CustomerId;
Name    Day    
----------  -----------
Paul Novak  2009-22-11 
Terry Neil  2009-28-11 
Terry Neil  2009-29-11 
Paul Novak  2009-29-11 
Jack Fonda  2009-02-12

We get the same data.

NATURAL INNER JOIN

The NATURAL INNER JOIN automatically uses all the matching column names for the join. In our tables, we have a column named CustomerId in both tables.

sqlite> SELECT Name, Day FROM Customers NATURAL JOIN Reservations;
Name     Day     
-----------  ----------
Paul Novak   2009-22-11
Terry Neils  2009-28-11
Terry Neils  2009-29-11
Paul Novak   2009-29-11
Jack Fonda   2009-02-12

We get the same data. The SQL statement is less verbose.

CROSS INNER JOIN

The CROSS INNER JOIN combines all records from one table with all records from another table. This type of join has little practical value. It is also called a cartesian product of records.

sqlite> SELECT Name, Day FROM Customers CROSS JOIN Reservations;
Name     Day     
-----------  ----------
Paul Novak   2009-22-11
Paul Novak   2009-28-11
Paul Novak   2009-29-11
Paul Novak   2009-29-11
Paul Novak   2009-02-12
Terry Neils  2009-22-11
Terry Neils  2009-28-11
Terry Neils  2009-29-11
Terry Neils  2009-29-11
Terry Neils  2009-02-12
...

The same result can be achieved with the following SQL statement:

sqlite> SELECT Name, Day FROM Customers, Reservations;

Outer joins

An outer join does not require each record in the two joined tables to have a matching record. There are three types of outer joins: left outer joins, right outer joins, and full outer joins. SQLite only supports left outer joins.

LEFT OUTER JOIN

The LEFT OUTER JOIN returns all values from the left table, even if there is no match with the right table. In such rows there will be NULL values. In other words, left outer join returns all the values from the left table, plus matched values from the right table. Note that the OUTER keyword can be omitted.

sqlite> SELECT Name, Day FROM Customers LEFT JOIN Reservations
   ...> ON Customers.CustomerId = Reservations.CustomerId;
Name     Day    
-----------  -----------
Paul Novak   2009-22-11 
Paul Novak   2009-29-11 
Terry Neils  2009-28-11 
Terry Neils  2009-29-11 
Jack Fonda   2009-02-12 
Tom Willis   NULL  

Here we have all customers with their reservations and a customer who has no reservation. There is a NULL value in his row.

We can use the USING keyword to achieve the same result. The SQL statement will be less verbose.

sqlite> SELECT Name, Day FROM Customers LEFT JOIN Reservations
   ...> USING (CustomerId);
Name     Day    
-----------  -----------
Paul Novak   2009-22-11 
Paul Novak   2009-29-11 
Terry Neils  2009-28-11 
Terry Neils  2009-29-11 
Jack Fonda   2009-02-12 
Tom Willis   NULL

We have the same result, with a shorter SQL statement.

NATURAL LEFT OUTER JOIN

The NATURAL LEFT OUTER JOIN automatically uses all the matching column names for the join.

sqlite> SELECT Name, Day FROM Customers NATURAL LEFT OUTER JOIN Reservations;
Name     Day     
-----------  ----------
Paul Novak   2009-22-11
Paul Novak   2009-29-11
Terry Neils  2009-28-11
Terry Neils  2009-29-11
Jack Fonda   2009-02-12
Tom Willis   NULL  

We have the same output but we have used fewer key strokes.

In this part of the SQLite tutorial, we were joining tables.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文