SQL Server 中左连接和右连接的区别

2023-07-17数据库问题
1

本文介绍了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 中左连接和右连接的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12