gpt4 book ai didi

mysql选择组中的最新时间戳

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

我有一个表,我想为其选择由表中特定列的值定义的类别中的最新时间戳。

特别是

SELECT * 
FROM takelist
WHERE producer_name = 'sean'
AND bucket_id = '2CCEx15_1o'

结果

+-------------+---------------+------------+---------------------+
| takelist_id | producer_name | bucket_id | ts |
+-------------+---------------+------------+---------------------+
| 1 | sean | 2CCEx15_1o | 2013-10-07 18:29:00 |
| 4 | sean | 2CCEx15_1o | 2013-10-07 18:33:09 |
| 5 | sean | 2CCEx15_1o | 2013-10-07 18:33:38 |
| 27 | sean | 2CCEx15_1o | 2013-10-07 18:37:38 |
| 212 | sean | 2CCEx15_1o | 2013-10-14 18:36:05 |
| 236 | sean | 2CCEx15_1o | 2013-10-21 17:59:56 |
| 237 | sean | 2CCEx15_1o | 2013-10-21 18:00:55 |
| 281 | sean | 2CCEx15_1o | 2013-10-29 15:58:40 |
| 287 | sean | 2CCEx15_1o | 2013-10-29 19:24:15 |
| 330 | sean | 2CCEx15_1o | 2013-10-31 14:39:33 |
| 615 | sean | 2CCEx15_1o | 2013-12-16 22:46:59 |
| 616 | sean | 2CCEx15_1o | 2013-12-16 22:54:46 |
+-------------+---------------+------------+---------------------+

我想为名为 bucket_id 的列的每个唯一值选择一行,其中所选行具有最新的时间戳。

我已经根据之前对类似问题的回答尝试了以下内容,但一定是出了什么问题

SELECT takes.* FROM takelist as takes 
INNER JOIN (
SELECT takelist_id, max(ts) max_ts, bucket_id
FROM takelist
WHERE producer_name='sean'
GROUP BY bucket_id
) latest_take
ON takes.takelist_id=latest_take.takelist_id
AND takes.ts=latest_take.max_ts

最佳答案

您的查询已结束。但是您使用的是 id 而不是时间戳:

SELECT takes.*
FROM takelist takes INNER JOIN
(SELECT max(ts) as max_ts, bucket_id
FROM takelist
WHERE producer_name = 'sean'
GROUP BY bucket_id
) latest_take
ON takes.ts = latest_take.max_ts and takes.bucket_id = latest_take.bucket_id;

在原始公式中选择了任意 takelist_id。它可能不是您想要的。

关于mysql选择组中的最新时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20832366/

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