Maison > base de données > tutoriel mysql > le corps du texte

初步介绍MySQL中的集合操作_MySQL

WBOY
Libérer: 2016-06-01 13:00:31
original
977 Les gens l'ont consulté

啥是集合操作?

通常来说,将联接操作看作是表之间的水平操作,因为该操作生成的虚拟表包含两个表中的列。而我这里总结的集合操作,一般将这些操作看作是垂直操作。MySQL数据库支持两种集合操作:UNION DISTINCT和UNION ALL。

与联接操作一样,集合操作也是对两个输入进行操作,并生成一个虚拟表。在联接操作中,一般把输入表称为左输入和右输入。集合操作的两个输入必须拥有相同的列数,若数据类型不同,MySQL数据库自动将进行隐式转换。同时,结果列的名称由左输入决定。
前期准备

准备测试表table1和table2:

create table table1 
      (aid int not null auto_increment, 
      title varchar(20), 
      tag varchar(10), 
      primary key(aid)) 
      engine=innodb default charset=utf8;

create table table2 
      (bid int not null auto_increment, 
      title varchar(20), 
      tag varchar(10), 
      primary key(bid)) 
      engine=innodb default charset=utf8;

Copier après la connexion

插入以下测试数据:

insert into table1(aid, title, tag) values(1, 'article1', 'MySQL');
insert into table1(aid, title, tag) values(2, 'article2', 'PHP');
insert into table1(aid, title, tag) values(3, 'article3', 'CPP');

insert into table2(bid, title, tag) values(1, 'article1', 'MySQL');
insert into table2(bid, title, tag) values(2, 'article2', 'CPP');
insert into table2(bid, title, tag) values(3, 'article3', 'C');

Copier après la connexion

UNION DISTINCT

UNION DISTINCT组合两个输入,并应用DISTINCT过滤重复项,一般可以直接省略DISTINCT关键字,直接使用UNION。

UNION的语法如下:

SELECT column,... FROM table1 
UNION [ALL]
SELECT column,... FROM table2
...

Copier après la connexion

在多个SELECT语句中,对应的列应该具有相同的字段属性,且第一个SELECT语句中被使用的字段名称也被用于结果的字段名称。

现在我运行以下sql语句:

(select * from table1) union (select * from table2);

Copier après la connexion

将会得到以下结果:

+-----+----------+-------+
| aid | title  | tag  |
+-----+----------+-------+
|  1 | article1 | MySQL |
|  2 | article2 | PHP  |
|  3 | article3 | CPP  |
|  2 | article2 | CPP  |
|  3 | article3 | C   |
+-----+----------+-------+

Copier après la connexion

我们发现,表table1和表table2中的重复数据项:

|  1 | article1 | MySQL |

Copier après la connexion

只出现了一次,这就是UNION的作用效果。

MySQL数据库目前对UNION DISTINCT的实现方式如下:

  • 创建一张临时表,也就是虚拟表;
  • 对这张临时表的列添加唯一索引;
  • 将输入的数据插入临时表;
  • 返回虚拟表。

因为添加了唯一索引,所以可以过滤掉集合中重复的数据项。这里重复的意思是SELECT所选的字段完全相同时,才会算作是重复的。

UNION ALL

UNION ALL的意思是不会排除掉重复的数据项,比如我运行以下的sql语句:

(select * from table1) union all (select * from table2);

Copier après la connexion

你将会得到以下结果:

+-----+----------+-------+
| aid | title  | tag  |
+-----+----------+-------+
|  1 | article1 | MySQL |
|  2 | article2 | PHP  |
|  3 | article3 | CPP  |
|  1 | article1 | MySQL |
|  2 | article2 | CPP  |
|  3 | article3 | C   |
+-----+----------+-------+

Copier après la connexion

发现重复的数据并不会被筛选掉。

在使用UNION DISTINCT的时候,由于向临时表中添加了唯一索引,插入的速度显然会因此而受到影响。如果确认进行UNION操作的两个集合中没有重复的选项,最有效的办法应该是使用UNION ALL。

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!