简体   繁体   English

SQL Server-从LEFT表返回所有记录,而从右表仅返回不匹配的记录

[英]SQL Server - Return all records from LEFT table and only non matching records from right table

I have 2 tables with the same structure (field names). 我有2个具有相同结构(表名)的表。 Table1 and Table2. 表1和表2。

I need to return all records from Table1 and only records from Table2 that do not match/join to a record in Table1. 我需要返回表1中的所有记录,并且仅返回表2中与表1中的记录不匹配/联接的记录。

Table2 has more records than Table1. 表2比表1具有更多的记录。

I am joining the 2 tables on 3 fields. 我将加入3个字段中的2个表。

So basically I want all records from table1 returned and only records that don't have a match (joining on the 3 fields) to table1 from table2 returned. 因此,基本上我希望返回table1的所有记录,并且只返回与table2的table1不匹配(在3个字段上连接)的记录。

Put another way, Table1 records take precedence over table2 records in my final result output when the records exist in both tables (same value for the 3 fields) 换句话说,当两个表中都存在记录时,表1记录的优先级高于最终结果输出中的表2记录(3个字段的值相同)

I started writing something like the below but I don't think it will work. 我开始写类似下面的内容,但我认为它不起作用。 Should I use a left outer join instead? 我应该改用左外部联接吗?

    Select * from table1 t1
    left join table2 t2 on t1.id = t2.id and t1.date = t2.date and t1.custid= t2.custid
where t2.id is null or t2.date is null or t2.custid is null

So, you need every row from table1 plus the rows from table2 that don't match with table1 ?: 因此,您需要table1每一行以及table2中与table1不匹配的行?

SELECT *
FROM table1
UNION ALL
SELECT *
FROM table2 t2
WHERE NOT EXISTS(SELECT * FROM table1
                 WHERE id = t2.id
                 AND date = t2.date
                 AND custid = t2.custid);
Select * from table1 t1
  Union
Select * from table2 t2
Where Not exists
     (Select * from table1 
      Where id = t1.id 
         and date = t1.date 
         and custid= t1.custid)

暂无
暂无

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

相关问题 根据与右表的匹配从左表中检索记录,也根据条件从右表中检索不匹配的记录 - Retrieve records from left table based on matching with right table and also non matching records from right table based on condition 从左表中获取所有记录并从右表中匹配记录的查询是什么? - What will be the query for fetching all the records from left table and matching records from the right tables? 返回左表中的所有记录,除了 - Return all records from left table except 如何仅从右表中检索没有匹配记录的左表值 - How to retrieve only left table values no matching records from right table 如何在 SQL 服务器中从第一个和第二个表中获取匹配记录,并且仅从第一个表中获取不匹配记录,该服务器已加入 1 个字段 - How to get a a matching records from 1st and 2nd table and only non matching records from 1st table in SQL Server having joined by 1 field SQL查询-获取左表中的所有匹配记录和剩余记录 - Sql query - get all matching records and remaining records in left table LEFT JOIN不会返回左侧表中的所有记录 - LEFT JOIN does not return all the records from the left side table 右联接不会返回右侧表中的所有记录 - Right Join does not return all the records from right side table SQL 左连接不包括左表中的所有记录 - SQL left join is not including all records from left table sql left join从left表中选择所有记录,即使rigth表中没有记录 - sql left join select all records from left table even if no records in rigth table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM