每个左连接返回 1 个结果

发布于 2024-09-24 06:28:08 字数 953 浏览 6 评论 0原文

目前我正在两个表上执行左连接。第一个表有一个 id 和一个人名,第二个表有一个 id,即表 1 中人员的 id,然后是时间戳(他们最后一次乘坐的航班的时间戳)。

People                             Flights
id   |  name             id   |   person_id   | time
------------             ---------------------------
1       Dave              1         1          1284762115
2       Becky             2         1          1284787352
                          3         2          1284772629
                          4         2          1286432934
                          5         1          1289239480

当我执行左连接时,我会得到人员及其飞行时间的列表,但我想要的只是人员及其上次飞行时间的列表。

所以 SELECT p.id, p.name, f.time FROM People p LEFT JOIN Flights f ON p.id = f.person_id

返回

1 Dave  1284762115
1 Dave  1284787352
1 Dave  1289239480
2 Becky 1284772629
2 Becky 1286432934

我只想看到:

1 Dave  1289239480
2 Becky 1286432934

所以我只需要返回具有最高 f.id 的匹配或最高 f.time

Currently I am performing a left join on two tables. The first table has an id and a persons name, the second table has an id, the id of a person from table 1, and then a timestamp (of the last flight they had).

People                             Flights
id   |  name             id   |   person_id   | time
------------             ---------------------------
1       Dave              1         1          1284762115
2       Becky             2         1          1284787352
                          3         2          1284772629
                          4         2          1286432934
                          5         1          1289239480

When I perform my left join, I get a list of people and their flight times, but what I would like is just the list of people with their last flight times.

So SELECT p.id, p.name, f.time FROM People p LEFT JOIN Flights f ON p.id = f.person_id

Returns

1 Dave  1284762115
1 Dave  1284787352
1 Dave  1289239480
2 Becky 1284772629
2 Becky 1286432934

I would like to see just:

1 Dave  1289239480
2 Becky 1286432934

So I need to return only the match with the highest f.id or the highest f.time

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

离线来电— 2024-10-01 06:28:08
SELECT
    p.id, p.name, MAX(f.time) AS LastFlight
FROM
    People p
    LEFT JOIN Flights f ON p.id = f.person_id
GROUP BY
    p.id, p.name
SELECT
    p.id, p.name, MAX(f.time) AS LastFlight
FROM
    People p
    LEFT JOIN Flights f ON p.id = f.person_id
GROUP BY
    p.id, p.name
夏至、离别 2024-10-01 06:28:08
SELECT p.id, p.name, f.time FROM People p LEFT JOIN 
  (select person_id, max(time) time from flights group by person_id) f 
ON p.id = f.person_id
SELECT p.id, p.name, f.time FROM People p LEFT JOIN 
  (select person_id, max(time) time from flights group by person_id) f 
ON p.id = f.person_id
岁月无声 2024-10-01 06:28:08

试试这个:

;with LastFlightTimes as 
(
select person_id, max(id) maxid
from Flights f
group by person_id
)
SELECT p.id, p.name, f.time FROM People p 
LEFT JOIN LastFlightTimes lft ON p.id = lft.person_id
left join Flights f on f.id = lft.maxid

Try this:

;with LastFlightTimes as 
(
select person_id, max(id) maxid
from Flights f
group by person_id
)
SELECT p.id, p.name, f.time FROM People p 
LEFT JOIN LastFlightTimes lft ON p.id = lft.person_id
left join Flights f on f.id = lft.maxid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文