gpt4 book ai didi

mysql - 获取 SQL 中的最新结果

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

我有以下表结构:

id    customer_id    version_num
1 1 1
2 1 2
3 2 1
4 2 2
5 3 1
6 3 2
7 3 3

我希望结果显示为:

id    customer_id    version_num
2 1 2
4 2 2
7 3 3

我能够得到以下结果:

id    customer_id    version_num
1 1 1
3 2 1
5 3 1

我使用的查询:

select * from TABLE group by customer_id

我不知道如何在查询中使用latest(version_num)。

最佳答案

SELECT Y.id, Y.customer_id, Y.version_num
FROM YourTable Y
JOIN (SELECT customer_id, MAX(version_num) max_v
FROM YourTable
GROUP customer_id
) T
ON Y.customer_id = T.customer_id
AND Y.version_num = T.max_v;

关于mysql - 获取 SQL 中的最新结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34074298/

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