一、内连接
满足条件的记录才会出现在结果集中。
二、 左外连接(left outer join,outer可省略)
左表全部出现在结果集中,若右表无对应记录,则相应字段为NULL
举例说明:
客户表:
订单表:
左外连接(LEFT OUTER JOIN)
select first_name, last_name, order_date, order_amount
from customers c
left join orders o
on c.customer_id = o.customer_id
结果:右表(order)只选取customer_id在左表出现过的结果(符合条件的order_date, order_amount,所以最后两行中date和amount都有NULL值)
三、右外连接(right outer join,outer可省略)
右表全部出现在结果集中,若左表无对应记录,则相应字段为NULL
举例说明:
select first_name, last_name, order_date, order_amount
from customers c
right join orders o
on c.customer_id = o.customer_id
结果:左表(customer)只选取customer_id在右表出现过的结果(符合条件的first_name和last_name,所以最后两行中first_name和last_name都有NULL值)
四、全连接(full outer join,outer可省略)
全外连接=左外连接+右外连接
举例:
select first_name, last_name, order_date, order_amount
from customers c
full join orders o
on c.customer_id = o.customer_id
结果:(左边有右边没有,和右边有左边没有的地方有NULL值)
参考:
https://www.pianshen.com/article/7828135881/
https://www.cnblogs.com/yyjie/p/7788413.html