我有下表和一些示例数据。
Record_ID Counter Serial Owner
1 0 AAA Jack
2 1 AAA Kevin
3 0 BBB Jane
4 1 BBB Wendy
基于与上述类似的数据,我正在尝试为 MySQL 编写一个 SQL 查询,以获取Counter
每个Serial
数字的最大值的记录。 我似乎遇到问题的部分是获取查询以获取最近更新的 50 个唯一序列号。
下面是我基于这个 StackOverflow question 提出的查询。
SELECT *
FROM `history` his
INNER JOIN(SELECT serial,
Max(counter) AS MaxCount
FROM `tracking`
WHERE serial IN (SELECT serial
FROM `history`)
GROUP BY serial
ORDER BY record_id DESC) q
ON his.serial = q.serial
AND his.counter = q.maxcount
LIMIT 0, 50