1、联结

联结运算,简单来说,就是将其他表中的列添加过来,进行“添加列”的运算。

2、内连接:inner join

--语法:
select A.1,A2,B1,B2
from A inner join B
on A3=B3

select sp.shop_id, sp.shop_name, sp.product_id, p.product_name,p.sale_price
from shopproduct as sp inner join product as p
on sp.product_id = p.product_id;
--将shopproduct表的shop_id, shop_name, product_id三列,与product表的product_name,sale_price两列进行联结

 注意:

  • 进行连接时需要在from子句中使用多张表
  • on之后指定两张表连接所使用的列(联结键),on是专门用来指定连接条件的,它能起到与where相同的作用。需要指定多个键时,同样可以使用and、or。在进行内连接时on子句是必不可少的,并且on必须书写在from和where之间。
  • 由于多表连接时,某个列到底属于哪张表比较容易混乱,因此采用了用“< 表的别名>.< 列名>”的形式来指定列。从语法上来说,只有那些同时存在于两张表中的列必须使用这样的书写方式,其他的列直接书写列名也不会发生错误。
  • 使用连接运算将满足相同规则的表连接起来时,where、group by、having、order by 等工具都可以正常使用。
  • 这张“表”只在select语句执行期间存在,select语句执行之后就会消失。

3、外连接:outer join 

--语法:
select A.1,A2,B1,B2
from A left(right) outer join B
on A3=B3

 外连接分为左外连接和右外连接,由关键字left和right指定。

product:

SQL SERVER 完全关联 sql 内关联_数据库

shopproduct:

SQL SERVER 完全关联 sql 内关联_mysql_02

 

左连接:A left outer join B是包括 A 表的全部数据,以及A表数据所在B表中对应的数据的 ,如果某条数据A表中存在,但是B表中不存在,会显示A表的数据,B表中的部分会为 null。

select sp.shop_id, sp.shop_name, sp.product_id, p.product_name,p.sale_price
from shopproduct as sp left outer join product as p
on sp.product_id = p.product_id;


+---------+-----------+------------+--------------+------------+
| shop_id | shop_name | product_id | product_name | sale_price |
+---------+-----------+------------+--------------+------------+
| 000A    | 东京      | 0001       | T恤衫        |       1000 |
| 000D    | 福冈      | 0001       | T恤衫        |       1000 |
| 000A    | 东京      | 0002       | 打孔器       |        500 |
| 000B    | 名古屋    | 0002       | 打孔器       |        500 |
| 000A    | 东京      | 0003       | 运动T恤      |       4000 |
| 000B    | 名古屋    | 0003       | 运动T恤      |       4000 |
| 000C    | 大阪      | 0003       | 运动T恤      |       4000 |
| 000B    | 名古屋    | 0004       | 菜刀         |       3000 |
| 000C    | 大阪      | 0004       | 菜刀         |       3000 |
| 000B    | 名古屋    | 0006       | 叉子         |        500 |
| 000C    | 大阪      | 0006       | 叉子         |        500 |
| 000B    | 名古屋    | 0007       | 擦菜板       |        880 |
| 000C    | 大阪      | 0007       | 擦菜板       |        880 |
+---------+-----------+------------+--------------+------------+

右连接:A right outer join B是包括B 表的全部数据,以及B表数据所在A表中对应的数据的 ,如果某条数据A表中不存在,会显示B表的数据,A表中的部分会为 null。

select sp.shop_id, sp.shop_name, sp.product_id, p.product_name,p.sale_price
from shopproduct as sp right outer join product as p
on sp.product_id = p.product_id;

+---------+-----------+------------+--------------+------------+
| shop_id | shop_name | product_id | product_name | sale_price |
+---------+-----------+------------+--------------+------------+
| 000A    | 东京      | 0001       | T恤衫        |       1000 |
| 000A    | 东京      | 0002       | 打孔器       |        500 |
| 000A    | 东京      | 0003       | 运动T恤      |       4000 |
| 000B    | 名古屋    | 0002       | 打孔器       |        500 |
| 000B    | 名古屋    | 0003       | 运动T恤      |       4000 |
| 000B    | 名古屋    | 0004       | 菜刀         |       3000 |
| 000B    | 名古屋    | 0006       | 叉子         |        500 |
| 000B    | 名古屋    | 0007       | 擦菜板       |        880 |
| 000C    | 大阪      | 0003       | 运动T恤      |       4000 |
| 000C    | 大阪      | 0004       | 菜刀         |       3000 |
| 000C    | 大阪      | 0006       | 叉子         |        500 |
| 000C    | 大阪      | 0007       | 擦菜板       |        880 |
| 000D    | 福冈      | 0001       | T恤衫        |       1000 |
| NULL    | NULL      | NULL       | 高压锅       |       6800 |
| NULL    | NULL      | NULL       | 圆珠笔       |        100 |
+---------+-----------+------------+--------------+------------+

全连接:(MySQL不支持)full join 或full outer join

完整外部连接返回左表和右表中的所有行。当某行在另一个表中没有匹配行时,则另一个表的选择列表列包含空值。如果表之间有匹配行,则整个结果集行包含左表和右表的数据值。

4、交叉连接(笛卡尔积)cross join

select <列名>,<列名>,....
from A crnss join B;

将A表中的每一行数据,都与B表中的每一行数据进行组合。如果A表中有3条数据,B表中有4条数据,那么进行交叉连接之后的总数据为3*4=12条数据