繁体   English   中英

从一个表中选择行,以一对多关系将另一表中的最新行连接起来

[英]Select rows from one table, join most recent row from other table with one-to-many relationship

我想做的是从一个表(表A)中选择一组特定的行,并与另一个表(表B)连接,这样,表A中只会出现一条记录,而表B中的最新记录会出现,基于datetime列。

例如,表A具有以下结构(大大简化了):

id | col_1     | col_2          
---+-----------+----------------
1  | something | something else 
2  | val_1     | val_2
3  | stuff     | ting
4  | goats     | sheep

表B如下所示:

id | fk_A      | datetime_col        | col_3
---+-----------+---------------------+--------
1  | 1         | 2012-02-01 15:42:14 | Note 1
2  | 1         | 2012-02-02 09:46:54 | Note 2
3  | 1         | 2011-11-14 11:18:32 | Note 3
4  | 2         | 2009-04-30 16:49:01 | Note 4
5  | 4         | 2013-06-21 15:42:14 | Note 5
6  | 4         | 2011-02-01 18:44:24 | Note 6

我想要的是一个看起来像这样的结果集:

id | col_1     | col_2          | datetime_col        | col_3
---+-----------+----------------+---------------------+--------
1  | something | something else | 2012-02-02 09:46:54 | Note 2
2  | val_1     | val_2          | 2009-04-30 16:49:01 | Note 4
3  | stuff     | ting           | NULL                | NULL
4  | goats     | sheep          | 2013-06-21 15:42:14 | Note 5

因此,您可以看到B.fk_A = A.id上的表B已与表A B.fk_A = A.id ,但是结果中仅包含B中最新的对应记录。

我尝试了SELECT DISTINCTLEFT JOIN和子查询的各种组合,但我无法使其正常工作,或者没有结果或类似的结果:

id | col_1     | col_2          | datetime_col        | col_3
---+-----------+----------------+---------------------+--------
1  | something | something else | 2012-02-01 15:42:14 | Note 1
1  | something | something else | 2012-02-02 09:46:54 | Note 2
1  | something | something else | 2011-11-14 11:18:32 | Note 3
2  | val_1     | val_2          | 2009-04-30 16:49:01 | Note 4
3  | stuff     | ting           | NULL                | NULL
4  | goats     | sheep          | 2013-06-21 15:42:14 | Note 5
4  | goats     | sheep          | 2011-02-01 18:44:24 | Note 6

...重复表A中的记录。

显然,我的SQL-fu不足以完成此任务,因此,如果您当中有人可以向我指出正确的方向,我将不胜感激。 我已经在Google上进行了大量的Google搜索和搜索工作,但没有找到与该特定任务相匹配的任何内容,尽管我确信之前曾有人问过这个问题-我怀疑有一个我遗忘/不知道的SQL关键字,并且如果我进行搜索,我会立即找到答案。

我认为这个问题也解决了相同的问题,尽管我不是100%肯定并且接受的答案涉及SELECT TOP ,我认为(?)在MySQL中无效。

由于我的实际查询要复杂得多,并且要连接多个表,因此我将在对其进行任何更改的情况下进行显示:

SELECT  `l` . * ,  `u`.`name` AS  'owner_name',  `s`.`name` AS  'acquired_by_name',  `d`.`type` AS  `dtype` ,  `p`.`type` AS  `ptype` 
FROM  `leads` l
LEFT JOIN  `web_users` u ON  `u`.`id` =  `l`.`owner` 
LEFT JOIN  `web_users` s ON  `s`.`id` =  `l`.`acquired_by` 
LEFT JOIN  `deal_types` d ON  `d`.`id` =  `l`.`deal_type` 
LEFT JOIN  `property_types` p ON  `p`.`id` =  `l`.`property_type`

该查询有效,并返回我想要的数据(有时我还添加了WHERE子句,但是效果很好),但是我现在想:

LEFT JOIN `notes` n ON  `n`.`lead_id` =  `l`.`id`

...其中notes包含“许多记录”, leads包含与它们相关的“一个记录”。

还应注意,潜在地,我也想返回最旧的记录(在不同的查询中),但我想这将是在某个地方反转ASC / DESC的简单情况,或类似的简单操作。

我认为这将帮助您:

SELECT A.id, A.col_1, A.col_2, A.datetime_col, A.col_3
FROM
    (SELECT B.id, B.col_1, B.col_2, C.datetime_col, C.col_3
    FROM tableA B LEFT OUTER JOIN tableB C ON B.id = C.id
    ORDER BY C.datetime_col desc) as A
GROUP BY A.id

暂无
暂无

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

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