赞
踩
连接一般定义为 R ∞ S A θ B \underset{A \theta B}{R \infty S} AθBR∞S,其中 ∞ \infty ∞ 表示连接运算符, θ \theta θ 为比较运算符, A θ B A\theta B AθB 是条件表达式。含义是从 R R R 和 S S S 的笛卡尔积 R × S R \times S R×S 中选取 R R R 关系在 A A A 属性组上的值与 S S S 关系在 B B B 属性组上的值满足比较关系 θ θ θ 的元组。
连接操作是数据库中用于将两个或多个表的数据行合并在一起的操作,常用于在不同表之间建立关联以进行查询。连接操作有多种类型,包括内连接、外连接以及交叉连接。
NULL
值。NULL
值。NULL
值。省份表 provinces
与 省会表 capital
:
-- 创建省份表 DROP TABLE IF EXISTS provinces; CREATE TABLE provinces ( province_name VARCHAR(255), country_name VARCHAR(255), capital_name VARCHAR(255) ); -- 插入省份数据 INSERT INTO provinces (province_name, country_name, capital_name) VALUES ('Guangdong', 'China', 'Guangzhou'), ('Sichuan', 'China', 'Chengdu'), ('Jiangsu', 'China', 'Nanjing'), ('California', 'USA', 'Sacramento'), ('Hawaii', 'USA', 'Honolulu'), ('Texas', 'USA', 'Houston');
province_name | country_name | capital_name |
---|---|---|
Guangdong | China | Guangzhou |
Sichuan | China | Chengdu |
Jiangsu | China | Nanjing |
California | USA | Sacramento |
Hawaii | USA | Honolulu |
Texas | USA | Houston |
-- 创建省会表 DROP TABLE IF EXISTS capital; CREATE TABLE capital ( capital_name VARCHAR(255), population INT ); -- 插入省会数据 INSERT INTO capital (capital_name, population) VALUES ('Guangzhou', 15000000), ('Nanjing', 8000000), ('Sacramento', 500000), ('Honolulu', 380000), ('Tokyo', 14000000), ('London', 9000000);
capital_name | population |
---|---|
Guangzhou | 15000000 |
Nanjing | 8000000 |
Sacramento | 500000 |
Honolulu | 380000 |
Tokyo | 14000000 |
London | 9000000 |
SELECT *
FROM provinces
JOIN capital ON provinces.capital_name = capital.capital_name;
province_name | country_name | provinces.capital_name | capital.capital_name | population |
---|---|---|---|---|
Guangdong | China | Guangzhou | Guangzhou | 15000000 |
Jiangsu | China | Nanjing | Nanjing | 8000000 |
California | USA | Sacramento | Sacramento | 500000 |
Hawaii | USA | Honolulu | Honolulu | 380000 |
SELECT *
FROM provinces
NATURAL JOIN capital;
capital_name | province_name | country_name | population |
---|---|---|---|
Guangzhou | Guangdong | China | 15000000 |
Nanjing | Jiangsu | China | 8000000 |
Sacramento | California | USA | 500000 |
Honolulu | Hawaii | USA | 380000 |
SELECT *
FROM provinces
LEFT JOIN capital ON provinces.capital_name = capital.capital_name;
province_name | country_name | capital_name | capital_name | population |
---|---|---|---|---|
Guangdong | China | Guangzhou | Guangzhou | 15000000 |
Sichuan | China | Chengdu | null | null |
Jiangsu | China | Nanjing | Nanjing | 8000000 |
California | USA | Sacramento | Sacramento | 500000 |
Hawaii | USA | Honolulu | Honolulu | 380000 |
Texas | USA | Houston | null | null |
SELECT *
FROM provinces
RIGHT JOIN capital ON provinces.capital_name = capital.capital_name;
province_name | country_name | capital_name | capital_name | population |
---|---|---|---|---|
Guangdong | China | Guangzhou | Guangzhou | 15000000 |
Jiangsu | China | Nanjing | Nanjing | 8000000 |
California | USA | Sacramento | Sacramento | 500000 |
Hawaii | USA | Honolulu | Honolulu | 380000 |
null | null | null | Tokyo | 14000000 |
null | null | null | London | 9000000 |
MySQL 不支持全外连接,这里只对相应结果进行展示。
province_name | country_name | capital_name | capital_name | population |
---|---|---|---|---|
Guangdong | China | Guangzhou | Guangzhou | 15000000 |
Sichuan | China | Chengdu | null | null |
Jiangsu | China | Nanjing | Nanjing | 8000000 |
California | USA | Sacramento | Sacramento | 500000 |
Hawaii | USA | Honolulu | Honolulu | 380000 |
Texas | USA | Houston | null | null |
null | null | null | Tokyo | 14000000 |
null | null | null | London | 9000000 |
交叉连接得到的是两张表数据的乘积,所以以下两种写法是等价的:
SELECT *
FROM provinces
CROSS JOIN capital;
SELECT *
FROM provinces, capital;
province_name | country_name | capital_name | capital_name | population |
---|---|---|---|---|
Texas | USA | Houston | Guangzhou | 15000000 |
Hawaii | USA | Honolulu | Guangzhou | 15000000 |
California | USA | Sacramento | Guangzhou | 15000000 |
Jiangsu | China | Nanjing | Guangzhou | 15000000 |
Sichuan | China | Chengdu | Guangzhou | 15000000 |
Guangdong | China | Guangzhou | Guangzhou | 15000000 |
Texas | USA | Houston | Nanjing | 8000000 |
Hawaii | USA | Honolulu | Nanjing | 8000000 |
California | USA | Sacramento | Nanjing | 8000000 |
Jiangsu | China | Nanjing | Nanjing | 8000000 |
Sichuan | China | Chengdu | Nanjing | 8000000 |
Guangdong | China | Guangzhou | Nanjing | 8000000 |
Texas | USA | Houston | Sacramento | 500000 |
Hawaii | USA | Honolulu | Sacramento | 500000 |
California | USA | Sacramento | Sacramento | 500000 |
Jiangsu | China | Nanjing | Sacramento | 500000 |
Sichuan | China | Chengdu | Sacramento | 500000 |
Guangdong | China | Guangzhou | Sacramento | 500000 |
Texas | USA | Houston | Honolulu | 380000 |
Hawaii | USA | Honolulu | Honolulu | 380000 |
California | USA | Sacramento | Honolulu | 380000 |
Jiangsu | China | Nanjing | Honolulu | 380000 |
Sichuan | China | Chengdu | Honolulu | 380000 |
Guangdong | China | Guangzhou | Honolulu | 380000 |
Texas | USA | Houston | Tokyo | 14000000 |
Hawaii | USA | Honolulu | Tokyo | 14000000 |
California | USA | Sacramento | Tokyo | 14000000 |
Jiangsu | China | Nanjing | Tokyo | 14000000 |
Sichuan | China | Chengdu | Tokyo | 14000000 |
Guangdong | China | Guangzhou | Tokyo | 14000000 |
Texas | USA | Houston | London | 9000000 |
Hawaii | USA | Honolulu | London | 9000000 |
California | USA | Sacramento | London | 9000000 |
Jiangsu | China | Nanjing | London | 9000000 |
Sichuan | China | Chengdu | London | 9000000 |
Guangdong | China | Guangzhou | London | 9000000 |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。