gpt4 book ai didi

sql - Postgres : Getting the highest matching log associated with a record?

转载 作者:行者123 更新时间:2023-11-29 14:22:36 25 4
gpt4 key购买 nike

我存储的日志如下:

LOG
ID | MODELID | EVENT
1 | 1 | Upped
2 | 1 | Downed
3 | 2 | Downed
4 | 1 | Upped
5 | 2 | Multiplexed
6 | 1 | Removed

然后我有以下模型:

MODEL
ID | NAME
1 | Model 1
2 | Model 2

我想以日志中与模型相关联的最高 ID 作为结果的日志条目结束:

NAME    | EVENT
Model 1 | Removed
Model 2 | Multiplexed

一个简单的连接给了我所有的结果:

SELECT * FROM MODEL AS M LEFT JOIN LOG AS L 
ON L.MODELID = M.ID

但这给了我所有的记录。我错过了什么?

最佳答案

试试这个

SELECT M.NAME,L.EVENT FROM LOG L INNER JOIN MODEL M
ON L.MODELID = M.ID
WHERE L.ID IN
(
SELECT MAX(ID) FROM LOG GROUP BY MODELID
)

关于sql - Postgres : Getting the highest matching log associated with a record?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com