简体   繁体   English

在主表中的时间之前加入最新记录

[英]Join most recent record before time in primary table

I know the "how to join the newest record" questions get asked a lot, however, this one exceeds my knowledge. 我知道“如何加入最新记录”的问题很多,但是,这超出了我的知识。 I usually use a "join the max date subquery and then join the row with the matching id and time" approach to the problem, and I can add additional conditions in the where clause of the subquery, but this style doesn't work in this situation. 我通常使用“加入最大日期子查询,然后将具有匹配的ID和时间的行加入行”来解决该问题,并且可以在子查询的where子句中添加其他条件,但是这种方式在这种情况下不起作用情况。

I am looking for a history of all records in the first table, and to join on the most recent record from the second table before the time of the record from the first. 我正在寻找第一个表中所有记录的历史记录,并在第一个表中的记录时间之前加入第二个表中的最新记录。

Example: 例:

CREATE TABLE `A` (
  `id` int(11) NOT NULL,
  `a_time` timestamp NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `A` (`id`, `a_time`) VALUES
(1, '2018-03-21 04:30:00'),
(2, '2018-03-21 05:30:00'),
(3, '2018-03-21 07:30:00'),
(4, '2018-03-21 12:30:00');

CREATE TABLE `B` (
  `id` int(11) NOT NULL,
  `b_time` timestamp NOT NULL,
  `some_text` varchar(128) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `B` (`id`, `b_time`, `some_text`) VALUES
(1, '2018-03-21 05:30:00', 'Foo'),
(2, '2018-03-21 09:30:00', 'Bar');


ALTER TABLE `A`
  ADD PRIMARY KEY (`id`);

ALTER TABLE `B`
  ADD PRIMARY KEY (`id`);

What I'm hoping for is the id and time from A, and the most recent (<=) some_text from B 我想要的是A的ID和时间,以及B的最新(<=)some_text

a.id a.a_time            b.some_text
------------------------------------
1    2018-03-21 04:30:00 null
2    2018-03-21 05:30:00 Foo
3    2018-03-21 07:30:00 Foo
4    2018-03-21 12:30:00 Bar

Can someone help me out with this one? 有人可以帮我这个忙吗? Thanks! 谢谢!

I would use a "correlated subquery" like this: 我将使用像这样的“相关子查询”:

Query 1 : 查询1

select
       a.*
    , (select b.some_text from b
       where b.b_time <= a.a_time
       order by b.b_time DESC
       limit 1) as some_text
from a
order by a.id

Results : 结果

| id |               a_time | some_text |
|----|----------------------|-----------|
|  1 | 2018-03-21T04:30:00Z |    (null) |
|  2 | 2018-03-21T05:30:00Z |       Foo |
|  3 | 2018-03-21T07:30:00Z |       Foo |
|  4 | 2018-03-21T12:30:00Z |       Bar |

I might use an uncorellated subquery, like this: 我可能会使用未分析的子查询,如下所示:

SELECT x.some
     , c.columns
     , y.some_column
  FROM 
     ( SELECT a.*
            , MAX(b.b_time) b_time 
         FROM a
         LEFT 
         JOIN b 
           ON b.b_time <= a.a_time 
        GROUP 
           BY id
     ) x 
  LEFT 
  JOIN b y 
    ON y.something = x.something;
+----+---------------------+-----------+
| id | a_time              | some_text |
+----+---------------------+-----------+
|  1 | 2018-03-21 04:30:00 | NULL      |
|  2 | 2018-03-21 05:30:00 | Foo       |
|  3 | 2018-03-21 07:30:00 | Foo       |
|  4 | 2018-03-21 12:30:00 | Bar       |
+----+---------------------+-----------+

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM