这是在左外连接中过滤右表的唯一方法吗?

时间:2014-04-11 12:19:10

标签: sql-server left-join

我将客户余额存储在自己的表中。客户余额表每天获取一组新记录(反映当天的余额),但包含其他日期的余额(yyyy-mm-dd)。我想从accountinformation获取balances的所有英国客户及其余额accountinformation。我希望包含来自balances的行,即使select firstname,lastname,accountnumber,balance from accountinformation i left outer join balances b on i.accountnumber = b.account where country = 'UK' and status = 'OPEN' and (b.date = '2014-04-10' or b.date is null) 中没有相应的记录(昨天)......

accountinformation

...如果balances中没有相应的行,则不满足显示select firstname,lastname,accountnumber,balance from accountinformation i left outer join (select * from balances where date = '2014-04-10') b on i.accountnumber = b.account where country = 'UK' and status = 'OPEN' 行的要求。我不得不写这样的查询...

left outer

..以获得理想的行为。为了正确起见,我想知道是否有更正确的方法来过滤{{1}}联接中的左表?

1 个答案:

答案 0 :(得分:2)

你可以做到

select firstname,lastname,accountnumber,balance from accountinformation i 
left outer join balances b 
on i.accountnumber = b.account and b.date = '2014-04-10'
where country = 'UK' and status = 'OPEN'