我想使用日期作为链接加入两个表,但是,我不希望完全匹配。第一个表的日期比第二个表的日期多,所以我希望匹配的日期从第二个表到最近的表。日期。
实施例
表1:
Observable.combineLatest(getWeather (), getLocation(), new Func2<List<Object_A>, List<Object_B>, Object>() {
@Override
public Object call(Object o, Object o2) {
combine both results and return the combine result to observer
}
})
表2:
Date1
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-05
2016-06-06
2016-06-07
加入表
Date2
2016-06-01
2016-06-05
2016-06-07
希望这是有道理的。
谢谢,
SR
答案 0 :(得分:1)
在MySql中,您可以使用相关的子查询:
SELECT Date1, (SELECT Date2
FROM Table2
WHERE Date2 <= t1.Date1
ORDER BY Date2 DESC LIMIT 1) AS Date2
FROM table1 AS t1
在SQL Server中,您可以使用CROSS APPLY
:
SELECT t1.Date1, t2.Date2
FROM table1 AS t1
CROSS APPLY (
SELECT TOP 1 Date2
FROM table2
WHERE Date2 <= t1.Date1
ORDER BY Date2 DESC) AS t2
答案 1 :(得分:0)
我假设您使用的是SQL Server。它类似于Giorgos Betsos帖子
Declare @Table1 Table(Date1 date)
Insert into @Table1 values
('2016-06-01')
,('2016-06-02')
,('2016-06-03')
,('2016-06-04')
,('2016-06-05')
,('2016-06-06')
,('2016-06-07')
Declare @Table2 Table(Date2 date)
Insert into @Table2 values
('2016-06-01')
,('2016-06-05')
,('2016-06-07')
SELECT Date1, (SELECT top 1 Date2
FROM @Table2
WHERE Date2 <= t1.Date1
order by Date2 desc) AS Date2
FROM @table1 AS t1