简体   繁体   English

根据多个列查找一个表中存在但另一个表中不存在的记录

[英]Find records that exists in one table but not another based on multiple columns

I have two tables, billing and weekly. 我有两个表,计费和每周。
I need to find values that are in the billing table but not in the weekly table, but I need to base it off of more than one column. 我需要找到帐单表格中的值,而不是每周表格中的值,但是我需要以多个列为基础。
I have a query that works for just one column: 我有一个查询仅适用于一列:

SELECT * from billing
outer apply
(select * from weekly where billing.[email]= weekly.[email])a
where a.[email] is null

Sometimes in my data the email can change. 有时我的数据中的电子邮件可能会更改。
So I need to add something if the email doesn't match, check first and last name but do not know how to do this. 因此,如果电子邮件不匹配,我需要添加一些内容,请检查名字和姓氏,但不知道该怎么做。

You could use the exists operator: 您可以使用exists运算子:

SELECT * 
FROM   billing
WHERE  NOT EXISTS
       (SELECT * 
        FROM   weekly 
        WHERE  billing.[email] = weekly.[email] OR
               (billing.[firstName] = weekly.[firstName] AND
                billing.[lastName] = weekly.[lastName]
               )
       )

You can use NOT EXISTS operator to exclude any records that match in weekly based on email OR first and last name. 您可以使用NOT EXISTS运算符根据电子邮件或姓氏和名字排除每周匹配的任何记录。

SELECT bl.*
        FROM billing bl
        WHERE NOT EXISTS (
                SELECT 1
                FROM weekly wk
                WHERE (bl.[email] = wk.[email]
                    OR (
                        bl.firstName = wk.firstName
                        AND bl.lastName = wk.lastName))
                    AND bl.lenderName <> wk.lenderName --added this check
                )

暂无
暂无

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

相关问题 在一个表中查找存在于另一个表的多个列中的值 - Find a value in one table that exists in multiple columns of another table 查询一个表中存在于另一个表中的两列中的记录 - Query records in one table that exists in either of two columns in another table SQL将一个表的多条记录合并成另一表的多列 - SQL combine multiple records of one table into multiple columns of another table 按多列显示一个表中的记录,该表不在另一个表中 - Display records from one table which is not in another table by multiple columns 根据列查找另一个表中不存在的数据 - Find data not exists in another table based on a column SQL-根据几列将记录从一个表匹配到另一表 - SQL - match records from one table to another table based on several columns 根据第三个表中的字段将一个表中的多个记录合并到另一个表中的一个字段中 - Combine multiple records in a table into one field in another table based on fields in a third table 根据另一个表中的记录选择一个表中的记录 - Select records from one table based on records from another table 根据多个条件,将记录从一个表中的特定列插入到另一表中 - inserting records from specific columns in one table to another table depending on multiple criteria 如何通过将一个表中的 id 与 bigquery 中另一个表中的多个列中的 id 连接来获取记录以获取大表? - How to get records by joining id in one table with id in multiple columns in another table in bigquery for huge tables?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM