gpt4 book ai didi

sql - 通过 FK 查找表中的最后记录

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

我的 table 看起来像:

id
caseid
status

表格中的示例数据:

1   10    open
2 10 published
3 10 reopened
4 11 open
5 11 contact
6 11 response
7 11 published

我需要获取最后状态为 published 的所有 caseid

所以在上面的例子中只有 11 会被检索(因为 caseid 10 稍后被重新结束)。

查询会是什么样子?

附言

我将 PDO 与准备好的语句一起使用。

最佳答案

select caseid from data d1 where d1.status = 'published' 
and not exists (select * from data d2 where d1.caseid = d2. caseid
and d2.id > d1.id)

为获得最佳性能,应为 id 和 caseid 列编制索引。

关于sql - 通过 FK 查找表中的最后记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8704511/

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