SQL Server中左联接和右联接之间的区别 [英] Difference between left join and right join in SQL Server

查看:95
本文介绍了SQL Server中左联接和右联接之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道SQL Server中的联接.

I know about joins in SQL Server.

例如.有两个表Table1,Table2.

For example. There are two tables Table1, Table2.

它们的表结构如下.

create table Table1 (id int, Name varchar (10))

create table Table2 (id int, Name varchar (10))

表1数据如下:

    Id     Name     
    -------------
    1      A        
    2      B    

Table2数据如下:

Table2 data as follows:

    Id     Name     
    -------------
    1      A        
    2      B 
    3      C

如果我同时执行下面提到的两个SQL语句,则两个输出将是相同的

If I execute both below mentioned SQL statements, both outputs will be the same

select *
from Table1
  left join Table2 on Table1.id = Table2.id

select *
from Table2
  right join Table1 on Table1.id = Table2.id

请在上述SQL语句中说明左右联接之间的区别.

Please explain the difference between left and right join in the above SQL statements.

推荐答案

Select * from Table1 left join Table2 ...

Select * from Table2 right join Table1 ...

确实是完全可以互换的.但是,请尝试尝试Table2 left join Table1(或同一对,Table1 right join Table2)以查看不同之处.此查询应为您提供更多行,因为Table2包含ID在Table1中不存在的行.

are indeed completely interchangeable. Try however Table2 left join Table1 (or its identical pair, Table1 right join Table2) to see a difference. This query should give you more rows, since Table2 contains a row with an id which is not present in Table1.

这篇关于SQL Server中左联接和右联接之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆