Home Database Mysql Tutorial 详解 SQL 对集合的操作:交、差、并

详解 SQL 对集合的操作:交、差、并

Jun 07, 2016 pm 04:14 PM
sql operate Detailed explanation gather

我认为Ligaya Turmelle的关于SQL联合(join)语句的帖子对于新开发者来说是份很好的材料。SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看是很自然而然的。不过正如在她的帖子的回复中所说的,在测试中我发现韦恩图并不是十分的匹配SQL联合语法。 不过

我认为Ligaya Turmelle的关于SQL联合(join)语句的帖子对于新开发者来说是份很好的材料。SQL 联合语句好像是基于集合的,用韦恩图来解释咋一看是很自然而然的。不过正如在她的帖子的回复中所说的,在测试中我发现韦恩图并不是十分的匹配SQL联合语法。

不过我还是喜欢这个观点,所以我们来看看能不能用上韦恩图。假设我们有下面两张表。表A在左边,表B在右边。我们给它们各四条记录。

1 2 3 4 5 6 id name       id  name -- ----       --  ---- 1  Pirate     1   Rutabaga 2  Monkey     2   Pirate 3  Ninja      3   Darth Vader 4  Spaghetti  4   Ninja

我们用过name字段用几种不同方式把这些表联合起来,看能否得到和那些漂亮的韦恩图在概念上的匹配。

1 2 3 4 5 6 7 8 SELECT * FROM TableA INNER JOIN TableB ON TableA.name = TableB.name   id  name       id   name --  ----       --   ---- 1   Pirate     2    Pirate 3   Ninja      4    Ninja

内联合(inner join)只生成同时匹配表A和表B的记录集。(如下图):交集

inner join

——————————————————————————-

1 2 3 4 5 6 7 8 9 10 11 12 SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name   id    name       id    name --    ----       --    ---- 1     Pirate     2     Pirate 2     Monkey     null  null 3     Ninja      4     Ninja 4     Spaghetti  null  null null  null       1     Rutabaga       null  null       3     Darth Vader

全外联合(full outer join)生成表A和表B里的记录全集,包括两边都匹配的记录。如果有一边没有匹配的,缺失的这一边为null。(如下图):并集

Full outer join

——————————————————————————-

1 2 3 4 5 6 7 8 9 10 SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name   id  name       id    name --  ----       --    ---- 1   Pirate     2     Pirate 2   Monkey     null  null 3   Ninja      4     Ninja 4   Spaghetti  null  null

左外联合(left outer join)生成表A的所有记录,包括在表B里匹配的记录。如果没有匹配的,右边将是null。(如下图)

Left outer join

——————————————————————————-

1 2 3 4 5 6 7 8 9 SELECT * FROM TableA LEFT OUTER JOIN TableB ON TableA.name = TableB.name WHERE TableB.id IS null   id  name       id     name --  ----       --     ---- 2   Monkey     null   null 4   Spaghetti  null   null

为了生成只在表A里而不在表B里的记录集,我们用同样的左外联合,然后用where语句排除我们不想要的记录。(如下图):差集

——————————————————————————-

1 2 3 4 5 6 7 8 9 10 11 12 SELECT * FROM TableA FULL OUTER JOIN TableB ON TableA.name = TableB.name WHERE TableA.id IS null OR TableB.id IS null   id    name       id    name --    ----       --    ---- 2     Monkey     null  null 4     Spaghetti  null  null null  null       1     Rutabaga null  null       3     Darth Vader

为了生成对于表A和表B唯一的记录集,我们用同样的全外联合,然后用where语句排除两边都不想要的记录。(如下图)

 

———————————————————–

 

还有一种笛卡尔积或者交叉联合(cross join),据我所知不能用韦恩图表示:

1 2 SELECT * FROM TableA CROSS JOIN TableB

 

这个把“所有”联接到“所有”,产生4乘4=16行,远多于原始的集合。如果你学过数学,你便知道为什么这个联合遇上大型的表很危险。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

Why is it difficult to implement collection-like functions in Go language? Why is it difficult to implement collection-like functions in Go language? Mar 24, 2024 am 11:57 AM

It is difficult to implement collection-like functions in the Go language, which is a problem that troubles many developers. Compared with other programming languages ​​such as Python or Java, the Go language does not have built-in collection types, such as set, map, etc., which brings some challenges to developers when implementing collection functions. First, let's take a look at why it is difficult to implement collection-like functionality directly in the Go language. In the Go language, the most commonly used data structures are slice and map. They can complete collection-like functions, but

Linux Deploy operation steps and precautions Linux Deploy operation steps and precautions Mar 14, 2024 pm 03:03 PM

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

Huawei Mate60 Pro screenshot operation steps sharing Huawei Mate60 Pro screenshot operation steps sharing Mar 23, 2024 am 11:15 AM

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Analysis of the impact of MySQL connection number on database performance Analysis of the impact of MySQL connection number on database performance Mar 16, 2024 am 10:09 AM

Analysis of the Impact of MySQL Connection Number on Database Performance With the continuous development of Internet applications, databases have become an important data storage and management tool to support application systems. In the database system, the number of connections is an important concept, which is directly related to the performance and stability of the database system. This article will start from the perspective of MySQL database, explore the impact of the number of connections on database performance, and analyze it through specific code examples. 1. What is the number of connections? The number of connections refers to the number of client connections supported by the database system at the same time. It can also be managed

Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Astar staking principle, income dismantling, airdrop projects and strategies & operation nanny-level strategy Jun 25, 2024 pm 07:09 PM

Table of Contents Astar Dapp Staking Principle Staking Revenue Dismantling of Potential Airdrop Projects: AlgemNeurolancheHealthreeAstar Degens DAOVeryLongSwap Staking Strategy & Operation "AstarDapp Staking" has been upgraded to the V3 version at the beginning of this year, and many adjustments have been made to the staking revenue rules. At present, the first staking cycle has ended, and the "voting" sub-cycle of the second staking cycle has just begun. To obtain the "extra reward" benefits, you need to grasp this critical stage (expected to last until June 26, with less than 5 days remaining). I will break down the Astar staking income in detail,

Forgot your Win8 computer startup password? This operation will restore it immediately! Forgot your Win8 computer startup password? This operation will restore it immediately! Mar 27, 2024 pm 10:12 PM

Forgetting the Win8 computer startup password is a problem that many people encounter when using computers on a daily basis. When we forget the login password, we will be unable to enter the system normally, causing inconvenience to our daily use. If you happen to encounter this problem, don’t worry. Below I will introduce some simple operations to help you quickly restore the power-on password of your Win8 computer. Method 1: Use Microsoft account password. If you use a Microsoft account to log in to your Win8 computer, you can try using the password of that account.

See all articles