关系代数——连接
文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://leander.blog.51cto.com/2911819/1094812
本节主要举例说明连接运算,包括内连接和外连接。
连接(或内连接)的结果是由组合两个操作数的匹配元组而形成的元组组成,外连接由这些元组加上通过向一个操作数的未匹配元组扩展上另一个操作数的每个属性的“填充”值而形成的元组组成。
外连接有三种: 左外连接、右外连接和全外连接。(有时省略“外”字)
如果加上又无NULL值,连接和外连接的形式如下图所示:
下面举例说明各种连接的情况:
- mysql> create table Name(id varchar(10), name varchar(10));
- Query OK, 0 rows affected (0.00 sec)
- mysql> create table Address(id varchar(10), address varchar(10));
- Query OK, 0 rows affected (0.01 sec)
- mysql> insert into Name values
- -> (‘10001‘,‘zhangsan‘),(‘10002‘,‘lisi‘), (‘10003‘,‘wangwu‘);
- Query OK, 3 rows affected (0.00 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> insert into Address values
- -> (‘10001‘,‘Beijing‘),(‘10003‘,‘Shanghai‘), (‘10009‘,‘Guangzhou‘);
- Query OK, 3 rows affected (0.00 sec)
- Records: 3 Duplicates: 0 Warnings: 0
- mysql> select * from Name;
- +-------+----------+
- | id | name |
- +-------+----------+
- | 10001 | zhangsan |
- | 10002 | lisi |
- | 10003 | wangwu |
- +-------+----------+
- 3 rows in set (0.00 sec)
- mysql> select * from Address;
- +-------+-----------+
- | id | address |
- +-------+-----------+
- | 10001 | Beijing |
- | 10003 | Shanghai |
- | 10009 | Guangzhou |
- +-------+-----------+
- 3 rows in set (0.00 sec)
连接或者内连接(inner join)
- mysql> select * from Name inner join Address using(id);
- +-------+----------+----------+
- | id | name | address |
- +-------+----------+----------+
- | 10001 | zhangsan | Beijing |
- | 10003 | wangwu | Shanghai |
- +-------+----------+----------+
- 2 rows in set (0.00 sec)
Left out-join,左外连接
自然连接∪左侧失配元组(右侧属性补空值)
- mysql> select * from Name left join Address using(id);
- +-------+----------+----------+
- | id | name | address |
- +-------+----------+----------+
- | 10001 | zhangsan | Beijing |
- | 10002 | lisi | NULL |
- | 10003 | wangwu | Shanghai |
- +-------+----------+----------+
- 3 rows in set (0.00 sec)
left out-join, B.key is NULL
- mysql> select * from Name left join Address using(id)
- -> where Address.address is NULL;
- +-------+---------+---------+
- | id | name | address |
- +-------+---------+---------+
- | 10002 | lisi | NULL |
- | NULL | zhaoliu | NULL |
- +-------+---------+---------+
- 2 rows in set (0.00 sec)
Right out-join ,右外连接
同左外连接反向
- mysql> select * from Name right join Address using(id);
- +-------+-----------+----------+
- | id | address | name |
- +-------+-----------+----------+
- | 10001 | Beijing | zhangsan |
- | 10003 | Shanghai | wangwu |
- | 10009 | Guangzhou | NULL |
- +-------+-----------+----------+
- 3 rows in set (0.00 sec)
Right out-join, A.key is NULL
- mysql> select * from Name right join Address using(id)
- -> where Name.name is NULL;
- +-------+-----------+------+
- | id | address | name |
- +-------+-----------+------+
- | 10009 | Guangzhou | NULL |
- | NULL | Wuhan | NULL |
- +-------+-----------+------+
- 2 rows in set (0.00 sec)
Full out-join,全外连接
自然连接
∪左侧失配元组(右侧属性补空值)
∪右侧失配元组(左侧属性补空值)
- mysql> select id, name, address from Name left join Address using(id)
- -> union
- -> select id, name, address from Name right join Address using(id);
- +-------+----------+-----------+
- | id | name | address |
- +-------+----------+-----------+
- | 10001 | zhangsan | Beijing |
- | 10002 | lisi | NULL |
- | 10003 | wangwu | Shanghai |
- | 10009 | NULL | Guangzhou |
- +-------+----------+-----------+
- 4 rows in set (0.00 sec)
Full join, A.key is NULL or B.key is NULL.
- mysql> (select id, name, address from Name left join Address using(id)
- -> where Address.address is NULL)
- -> union
- -> (select id, name, address from Name right join Address using(id)
- -> where Name.name is NULL);
- +-------+---------+-----------+
- | id | name | address |
- +-------+---------+-----------+
- | 10002 | lisi | NULL |
- | NULL | zhaoliu | NULL |
- | 10009 | NULL | Guangzhou |
- | NULL | NULL | Wuhan |
- +-------+---------+-----------+
- 4 rows in set (0.00 sec)
本文出自 “猫眼看世界” 博客,请务必保留此出处http://leander.blog.51cto.com/2911819/1094812
时间: 2024-11-05 16:23:51