简体   繁体   English

当一个表具有大量数据而另一个表具有少量数据时,在SQL Server中联接两个表

[英]Joining two tables in SQL Server when one table has huge data and other has few data

If I want to join two tables (not inner join), Left table has huge data (millions of record), and right table has few records. 如果我要联接两个表(而不是内部联接),则左表具有巨大的数据(数百万条记录),而右表具有很少的记录。 What should I prefer (Left or Right Outer join) and why. 我应该选择哪种(左或右外部联接)以及原因。

Well first of all That join is independent of the size of the tables 首先,该联接与表的大小无关

Well I think this depends what data you want either from Left table or from right table let us assume you have two tables Employees which has millions of records let us put this in right and Department which has 10 record put it in left Now each employee has one department. 好吧,我认为这取决于您要从“左”表还是“右”表中获取什么数据,让我们假设您有两个表,拥有数百万条记录的员工将其放到右边,将拥有10条记录的部门放到左边,现在每个员工都有一个部门。

Employee 雇员

  • EmpID 的EmpID
  • DepartmentId DepartmentID的

    Department

  • DepartmentId DepartmentID的

  • Department Name 部门名称

Now Suppose You want to know which employee belongs to which Department Use This Query. 现在假设您想知道哪个雇员属于哪个部门使用此查询。

Select e.empId,d.DepartmentName
from employee e
join department d
on e.departmentid=d.departmentid

Now Suppose You want to know which employee has now assigned any department use the below query 现在假设您想知道哪个员工现在已经分配了任何部门,请使用以下查询

 Select e.empId,d.DepartmentName
from employee e
left join department d
on e.departmentid=d.departmentid
where d.departmentid is null

Now Suppose You want to know how many employees which depratment use the below query 现在假设您想知道有多少贬低的员工使用以下查询

Select d.[Department Name],COUNT(e.empID) from Employee e
left join Department d
on e.DepartmentId=d.DepartmentId
group by d.[Department Name]

For more information about joins please use the below image 有关加入的更多信息,请使用下图 Sql联接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当一个表有多个对应值时连接两个表 - Joining two tables when one table has multiple corresponding values SQL 查询:连接两个表,其中第一个表的实体没有,或另一个表中有多个条目 - SQL Query: Joining two tables where entity of the first table has no, or multiple entries in other table 当一张表有空值时连接数据 - Joining data when one table has null value 为什么 SQL 查询在连接两个表并且一个表有很多行时执行时间太长 - Why SQL query takes too long time to execute when joining two tables and one table has lot of rows SQL How to:从两个在其他命名列中具有2个相同数据的表中,需要对其中一个进行计数 - SQL How to: from two tables that has 2 same data in other named columns, one of them need to be counted 连接 SQL 中的两个表,其中一列必须“清理” - Joining two tables in SQL in which one column has to be “cleaned” 当一个表的约束键具有很少的空值时,如何合并两个表的结果 - How to combine results of two tables when constraint key of one table has few null values SQL 语句检查数据是否存在于一个表中,而不存在于其他两个表中 - SQL statement to check if data exists in one table and not in two other tables Sql 服务器,将数据插入到两个表中,一个在另一个之前填充 - Sql server, insert data into two tables, populate one before the other 将表连接到SQL Server中的两个一对多关系表 - Joining a table to two one-to-many relationship tables in SQL Server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM