在 table 部分数据上以不同条件左连接两个 table

Left join on two tables with different conditions on part of table data

我想使用左连接来连接两个 table。我想将一个条件放在数据的一部分上,将另一个条件放在其余数据上。有点乱,举个例子解释一下:

Table  - a
 Date        ID    name
2015-04-30  Y235J   P54
2015-04-30  Y237F   P54
2015-04-30  Y234A   P54
2015-04-30  Y235    P54
2015-04-30  Y239    P54
2015-04-30  Y297    P54
2015-04-30  L234    P54
2015-04-30  L236    P54
2015-04-30  M234    P54
2015-04-30  M237    P54
2015-04-30  M239    P54
2015-04-30  M238    P54
2015-05-31      
2015-06-30      
2015-07-31      
2015-08-31      
2015-09-30      
2015-10-31      

Table -b

Date1        ID1 
2015-04-30  Y235
2015-04-30  Y239
2015-04-30  L234
2015-04-30  M237
2015-04-30  M239
2015-05-31  B435
2015-05-31  B486
2015-06-30  B435
2015-06-30  B486
2015-06-30  B477
2015-07-31  G456
2015-07-31  G345
2015-07-31  B486
2015-07-31  B477

我想左连接 table a 和 table b。我想放置 table a left joins with table b based on Date and ID if Date1 <= '2015-04- 30' 并且基于 just Date if Date1 > '2015-04-30'

请问这种情况怎么处理

    Select * from a left joins b (If b.Date1 <= '2015-04-30' then on 
a.Date = b.Date1 and a.ID = b.ID else on a.Date = b.Date1)

我无法得到答案。 谢谢,

您可以将此条件表示为:

Select *
from a left join
     b 
     on a.Date = b.Date1 and
        (b.Date1 > '2015-04-30' or  and a.ID = b.ID)
select * 
from a 
left join b
on a.ID = b.ID
and a.Date = b.Date1
where b.Date1 > '2015-04-30';