我在SQL中有两个表,并且我需要能够基于早于或等于表A中的时间戳的表B中的时间戳进行联接。
因此,这是两个表和所需输出的一些虚假数据:
结案案例(表A)
| id | resolution | timestamp | ------------------------------------------------ | 1 | solved | 2006-10-05 11:55:44.888153 | | 2 | closed | 2007-10-07 12:34:17.033498 | | 3 | trashed | 2008-10-09 08:19:36.983747 | | 4 | solved | 2010-10-13 04:28:14.348753 |
分类(表B)
| id | value | timestamp | ------------------------------------------------- | 1 | freshman | 2006-01-01 12:02:44.888153 | | 2 | sophomore | 2007-01-01 12:01:19.984333 | | 3 | junior | 2008-01-01 12:02:28.746149 |
所需结果
| id | resolution | timestamp | value | -------------------------------------------------------------- | 1 | solved | 2006-10-05 11:55:44.888153 | freshman | | 2 | closed | 2007-10-07 12:34:17.033498 | sophomore | | 3 | trashed | 2008-10-09 08:19:36.983747 | junior | | 4 | solved | 2010-10-13 04:28:14.348753 | junior |
因此,我知道代码需要如下所示,我只是想不出该如何处理ON部分JOIN($ 1和$ 2是将要传入的变量):
ON
JOIN
SELECT case.id, case.resolution, case.timestamp, class.value FROM closed_cases AS case LEFT JOIN classifications AS class ON ??? WHERE case.timestamp BETWEEN $1 AND $2;
我知道我可以使用子选择,但这将在至少几千行,甚至可能更多的行上进行,我需要它真的要快。所以我希望有一个简单的子句可以做到。
如果可以更改表的结构,建议更改分类表以包括结束日期和开始日期-这样将更容易加入表。
如果没有,我建议如下:
SELECT case.id, case.resolution, case.timestamp, class.value FROM closed_cases AS case LEFT JOIN (select c.*, (select min(timestamp) from classifications c1 where c1.timestamp > c.timestamp) timeend from classifications c) AS class ON case.timestamp >= class.timestamp and (case.timestamp < class.timeend or class.timeend IS NULL) WHERE case.timestamp BETWEEN $1 AND $2;
编辑-结束日期为分类:
SELECT case.id, case.resolution, case.timestamp, class.value FROM closed_cases AS case LEFT JOIN classifications AS class ON case.timestamp >= class.timestamp and case.timestamp < class.timeend WHERE case.timestamp BETWEEN $1 AND $2;