【发布时间】:2015-05-02 21:14:48
【问题描述】:
我有一个现有的查询来选择一些付款
我想过滤掉针对在另一个名为 ClientAlert 的表中具有活动警报的客户的任何付款
所以我想我会做一个左连接并检查 ClientAlertId 是否为空。
select *
from payments p
left join client c on c.clientid = p.clientid
left join ClientAlert ca on ca.CRMId = c.CRMId and ca.ClientAlertSubjectId = 1 and ca.IsActive = 1 and (ca.ExpiryDate is null or ca.ExpiryDate > GetDate())
where
ca.clientalertid is null and
p.PaymentStatusId = 2 and
p.PaymentDate <= GetDate() and
p.PaymentCategoryId = 1
我认为这似乎可行
但我有两个问题:
是否存在通过添加此联接导致返回多笔付款而不是一次付款的情况?
-
当我在 where 子句而不是 join 中指定以下内容时,它没有给出相同的结果,我不明白为什么
and ca.ClientAlertSubjectId = 1 and ca.IsActive = 1 and (ca.ExpiryDate is null or ExpiryDate > GetDate())
我认为在 where 子句中使用该标准将等同于在连接中使用它
【问题讨论】:
-
I thought having that criteria in the where clause woiuld be equivelent to having it in the join,INNER JOIN可能是这样,但OUTER JOIN是绝对错误的 -
请编辑您的问题并使用表格别名限定列名。对于外连接,列的来源有很大的不同。
-
@GordonLinoff - 完成
标签: sql sql-server