[英]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.