Home Database Mysql Tutorial SQL中JOIN和UNION区别、用法及示例介绍

SQL中JOIN和UNION区别、用法及示例介绍

Jun 07, 2016 pm 06:07 PM
join union

join 是两张表做交连后里面条件相同的部分记录产生一个记录集,union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集

1.JOIN和UNION区别

join 是两张表做交连后里面条件相同的部分记录产生一个记录集,
union是产生的两个记录集(字段要一样的)并在一起,成为一个新的记录集 。

JOIN用于按照ON条件联接两个表,主要有四种:
INNER JOIN:内部联接两个表中的记录,仅当至少有一个同属于两表的行符合联接条件时,内联接才返回行。我理解的是只要记录不符合ON条件,就不会显示在结果集内。
LEFT JOIN / LEFT OUTER JOIN:外部联接两个表中的记录,并包含左表中的全部记录。如果左表的某记录在右表中没有匹配记录,则在相关联的结果集中右表的所有选择列表列均为空值。理解为即使不符合ON条件,左表中的记录也全部显示出来,且结果集中该类记录的右表字段为空值。
RIGHT JOIN / RIGHT OUTER JOIN:外部联接两个表中的记录,并包含右表中的全部记录。简单说就是和LEFT JOIN反过来。
FULL JOIN / FULL OUTER JOIN:完整外部联接返回左表和右表中的所有行。就是LEFT JOIN和RIGHT JOIN和合并,左右两表的数据都全部显示。

JOIN的基本语法:
Select table1.* FROM table1 JOIN table2 ON table1.id=table2.id

sql写法

内连接inner join:
代码如下:
SELECT msp.name, party.name
FROM msp JOIN party ON party=code


代码如下:
SELECT msp.name, party.name
FROM msp inner JOIN party ON party=code


左连接left join :
代码如下:
SELECT msp.name, party.name
FROM msp LEFT JOIN party ON party=code

右连接right join :
代码如下:
SELECT msp.name, party.name
FROM msp RIGHT JOIN party ON msp.party=party.code

全连接(full join):
代码如下:
SELECT msp.name, party.name
FROM msp FULL JOIN party ON msp.party=party.code


UNION运算符

将两个或更多查询的结果集组合为单个结果集,该结果集包含联合查询中的所有查询的全部行。UNION的结果集列名与UNION运算符中第一个Select语句的结果集的列名相同。另一个Select语句的结果集列名将被忽略。
其中两种不同的用法是UNION和UNION ALL,区别在于UNION从结果集中删除重复的行。如果使用UNION ALL 将包含所有行并且将不删除重复的行。

UNION和UNION ALL的区别:

union 检查重复
union all 不做检查
比如 select 'a' union select 'a' 输出就是一行 a
比如 select 'a' union all select 'a' 输出就是两行 a

2. 通过下面的例子,可以清晰的看出和理解2者的区别
实例1 典型的二表连接演示

假定有两个表Table1和Table2,其包含的列和数据分别如表1.1和表1.2所示。

表1.1 Table1数据库表

表1.2 Table2数据库表

Table1和Table2表共有的列为ColumnA,如果通过ColumnA列的值连接Table1和Table2两个表,即连接条件为Table1.ColumnA=Table2.ColumnA,此时得到的连接结果如表1.3所示。

表1.3 连接Table1和Table2表

上述连接过程的实现代码可表示如下:SELECT * FROM Table1 JOIN Table2 ON Table1.ColumnA=Table2.columnA

实例2 典型的二表记录的UNION运算

假定有两个表Table3和Table4,其包含的列和数据分别如表2.1和表2.2所示。

表2.1 Table3数据库表

表2.2 Table4数据库表、

Table3表和Table4表具有相同的列结构,列数也要相同,列名可以不同,以第一个表的列名为新表的列名,因此可以使用UNION运算符连接两个表的记录集,得到的连接结果如表2.3所示。

表2.3 使用UNION连接Table3表和Table4表的记录

上述连接过程的实现代码可表示如下:SELECT * FROM Table3 UNION SELECT *FROM Table4

对比实例1和实例2,不难发现二者的区别。

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Will R.E.P.O. Have Crossplay?
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to use JOIN in MySql How to use JOIN in MySql Jun 04, 2023 am 08:02 AM

The meaning of JOIN is just like the English word "join". It joins two tables and can be roughly divided into inner join, outer join, right join, left join and natural join. First create two tables, the following is used as an example CREATETABLEt_blog(idINTPRIMARYKEYAUTO_INCREMENT,titleVARCHAR(50),typeIdINT);SELECT*FROMt_blog;+----+------+--------+| id|title|typeId|+----+-------+--------+|1|aaa|1||2|bbb|2||3|ccc|3|

What is the usage principle of MySQL Join? What is the usage principle of MySQL Join? May 26, 2023 am 10:07 AM

Join type leftjoin uses the left table as the driving table and the left table as the basis of the result set. The data from the right table is connected to the result set. rightjoin uses the right table as the driving table and the right table as the basis of the result set to connect the left table. The data is added to the result set innerjoin. The result set takes the intersection of the two tables fulljoin. The result set takes the union of the two tables. MySQL does not have a fulljoin. The difference between union and unionall is that union will deduplicate the crossjoin Cartesian product. If the where condition is not used, the result set will be the product and of the two associated table rows. The difference is that when crossjoin creates the result set, it will be passed according to the on condition.

How to use union in c language How to use union in c language Sep 27, 2023 am 11:00 AM

The use of union in C language is a special data type that allows different data types to be stored in the same memory location. The use of union can help us save memory space and facilitate conversion between different data types. When using union, you need to note that the corresponding member is valid and only one member can be accessed at the same time.

How to use JOIN in MySQL How to use JOIN in MySQL Jun 03, 2023 am 09:30 AM

Introduction A's unique + AB's public B's unique + AB's public AB's public A's unique B's unique A's unique + B's unique + AB's public A's unique + B's unique Practice creating table department tables DROPTABLEIFEXISTS`dept`;CREATETABLE`dept`(`dept_id`int(11)NOTNULLAUTO_INCREMENT,`dept_name`varchar(30)DEFAULTNULL,`dept_number`int(11)DEFAULTNULL,PRIMARYKEY(`dept_id`))ENGINE =InnoDBAUT

What are mysql's join query and multiple query methods? What are mysql's join query and multiple query methods? Jun 02, 2023 pm 04:29 PM

Compared with join query and multiple queries, which one is more efficient, MySQL multi-table related query or multiple single-table query? When the amount of data is not large enough, there is no problem using join, but it is usually done on the service layer. First: the computing resources of a stand-alone database are very expensive, and the database needs to serve both writing and reading at the same time, which requires CPU consumption. In order to make the database The throughput becomes higher, and the business does not care about the delay gap of hundreds of microseconds to milliseconds. The business will put more calculations into the service layer. After all, computing resources can be easily expanded horizontally, and databases are difficult, so most The business will put pure computing operations into the service layer, and use the database as a kv system with transaction capabilities. This is a heavy business.

How to use the FULL OUTER JOIN function in MySQL to obtain the union of two tables How to use the FULL OUTER JOIN function in MySQL to obtain the union of two tables Jul 26, 2023 pm 05:45 PM

How to use the FULLOUTERJOIN function in MySQL to obtain the union of two tables. In MySQL, the FULLOUTERJOIN function is a powerful join operation that combines inner joins and outer joins. It can be used to get the union of two tables, that is, combine all the data in the two tables into a single result set. This article will introduce the usage of the FULLOUTERJOIN function and provide some sample code to help readers better understand. FULLOUTERJOIN function

How to use mysql union to implement full outer join query How to use mysql union to implement full outer join query May 30, 2023 pm 06:49 PM

1. Union is not a method of multi-table connection query. It combines the query results of multiple query sentences into one result and removes duplicate data. 2. Full outer join queries the data of the left table and the right table, and then connects according to the connection conditions. Example #Use the left outer Aunion and the right outer BSELECT*FROMt_categorycLEFTOUTERJOINt_productpONc.cid=p.cnounionSELECT*FROMt_categorycRIGHTOUTERJOINt_productpONc.cid=p.cno

How does java define the Union class to realize the coexistence of data bodies? How does java define the Union class to realize the coexistence of data bodies? May 14, 2023 pm 03:34 PM

Define the Union class to implement the coexistence of data bodies. In the C/C++ language, a union, also known as a union, is a data structure similar to a struct. A union, like a struct, can contain many data types and variables. The difference between the two is as follows: all variables in a struct "coexist", and all variables are effective at the same time. Each variable occupies Different memory spaces; in a union, each variable is "mutually exclusive", only one variable is effective at the same time, and all variables occupy the same memory space. When multiple data need to share memory or only one of multiple data needs to be taken at a time, a union can be used. in Java

See all articles