gpt4 book ai didi

mysql - 获取最新的二级标识符 "value"

转载 作者:行者123 更新时间:2023-11-29 02:24:26 24 4
gpt4 key购买 nike

我需要使用特定标识符 (IDb) 选择最后一个值(具有最高 IDa)。

例如-输入:

|IDa|IDb| text
================
| 1 | 1 | What
| 2 | 2 | code
| 3 | 1 | should
| 4 | 2 | I
| 5 | 2 | write
| 6 | 3 | here
| 7 | 2 | ?

想要的输出:

|IDa|IDb| text
================
| 3 | 1 | should
| 7 | 2 | ?
| 6 | 3 | here

感谢您的帮助。

最佳答案

如果 IDa 设置为 auto_increment 你可以使用自连接

select t.*
from t
join (select max(IDa) IDa,IDb from t group by IDb ) t1
using(IDa,IDb) /* is equivalent to on(t.IDa = t1.IDa and t.IDb = t1.IDb) */
order by t.IDb

此外,在您的预期结果集中,您还提到了 IDb 1 的 what,根据您的问题,应该有 should 一词,因为它是 IDb 1 的最后一个

Fiddle Demo

关于mysql - 获取最新的二级标识符 "value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25351492/

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