sql的四种连接——左外连接、右外连接、内连接、全连接

一、内连接

  满足条件的记录才会出现在结果集中。

sql的四种连接——左外连接、右外连接、内连接、全连接

 

二、 左外连接(left outer join,outer可省略)

左表全部出现在结果集中,若右表无对应记录,则相应字段为NULL

 sql的四种连接——左外连接、右外连接、内连接、全连接

 

举例说明:

客户表:
sql的四种连接——左外连接、右外连接、内连接、全连接

订单表:
sql的四种连接——左外连接、右外连接、内连接、全连接

左外连接(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值)sql的四种连接——左外连接、右外连接、内连接、全连接

 

 

三、右外连接(right outer join,outer可省略)

  右表全部出现在结果集中,若左表无对应记录,则相应字段为NULL

sql的四种连接——左外连接、右外连接、内连接、全连接

 

 

举例说明:

  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值)
sql的四种连接——左外连接、右外连接、内连接、全连接

 

四、全连接(full outer join,outer可省略)

  全外连接=左外连接+右外连接

sql的四种连接——左外连接、右外连接、内连接、全连接

 

举例:

  select first_name, last_name, order_date, order_amount

  from customers c

  full join orders o

  on c.customer_id = o.customer_id

结果:(左边有右边没有,和右边有左边没有的地方有NULL值)

sql的四种连接——左外连接、右外连接、内连接、全连接

 

 

参考:

https://www.pianshen.com/article/7828135881/

https://www.cnblogs.com/yyjie/p/7788413.html

sql的四种连接——左外连接、右外连接、内连接、全连接

上一篇:adb常用命令(一)


下一篇:Caused by:com.rabbitmq.client.ShutdownSignalException: connection error;(reply-code=530, reply-text=NOT_ALLOWED - access to vhost '/' refused for user 'admin'