MySQL 查询,MAX() +分组依据
愚蠢的 SQL 问题。我有一个像这样的表(“pid”是自动递增主列)
CREATE TABLE theTable (
`pid` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`cost` INT UNSIGNED NOT NULL,
`rid` INT NOT NULL,
) Engine=InnoDB;
实际表数据:
INSERT INTO theTable (`pid`, `timestamp`, `cost`, `rid`)
VALUES
(1, '2011-04-14 01:05:07', 1122, 1),
(2, '2011-04-14 00:05:07', 2233, 1),
(3, '2011-04-14 01:05:41', 4455, 2),
(4, '2011-04-14 01:01:11', 5566, 2),
(5, '2011-04-14 01:06:06', 345, 1),
(6, '2011-04-13 22:06:06', 543, 2),
(7, '2011-04-14 01:14:14', 5435, 3),
(8, '2011-04-14 01:10:13', 6767, 3)
;
我想获取每个rid的最新行的PID(每个唯一的RID有1个结果)。对于示例数据,我想:
pid | MAX(timestamp) | rid
-----------------------------------
5 | 2011-04-14 01:06:06 | 1
3 | 2011-04-14 01:05:41 | 2
7 | 2011-04-14 01:14:14 | 3
我尝试运行以下查询:
SELECT MAX(timestamp),rid,pid FROM theTable GROUP BY rid
并且我得到:
max(timestamp) ; rid; pid
----------------------------
2011-04-14 01:06:06; 1 ; 1
2011-04-14 01:05:41; 2 ; 3
2011-04-14 01:14:14; 3 ; 7
返回的 PID 始终是 RID 的 PID 的第一次出现(行/pid 1 是第一次使用 rod 1,行/pid 3 是第一次使用 RID 2,row /pid 7 是第一次使用 rod 3)。虽然返回每个 Rid 的最大时间戳,但 pid 不是原始表中时间戳的 pid。什么查询会给我我正在寻找的结果?
Daft SQL question. I have a table like so ('pid' is auto-increment primary col)
CREATE TABLE theTable (
`pid` INT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
`timestamp` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`cost` INT UNSIGNED NOT NULL,
`rid` INT NOT NULL,
) Engine=InnoDB;
Actual table data:
INSERT INTO theTable (`pid`, `timestamp`, `cost`, `rid`)
VALUES
(1, '2011-04-14 01:05:07', 1122, 1),
(2, '2011-04-14 00:05:07', 2233, 1),
(3, '2011-04-14 01:05:41', 4455, 2),
(4, '2011-04-14 01:01:11', 5566, 2),
(5, '2011-04-14 01:06:06', 345, 1),
(6, '2011-04-13 22:06:06', 543, 2),
(7, '2011-04-14 01:14:14', 5435, 3),
(8, '2011-04-14 01:10:13', 6767, 3)
;
I want to get the PID of the latest row for each rid (1 result per unique RID). For the sample data, I'd like:
pid | MAX(timestamp) | rid
-----------------------------------
5 | 2011-04-14 01:06:06 | 1
3 | 2011-04-14 01:05:41 | 2
7 | 2011-04-14 01:14:14 | 3
I've tried running the following query:
SELECT MAX(timestamp),rid,pid FROM theTable GROUP BY rid
and I get:
max(timestamp) ; rid; pid
----------------------------
2011-04-14 01:06:06; 1 ; 1
2011-04-14 01:05:41; 2 ; 3
2011-04-14 01:14:14; 3 ; 7
The PID returned is always the first occurence of PID for an RID (row / pid 1 is frst time rid 1 is used, row / pid 3 the first time RID 2 is used, row / pid 7 is first time rid 3 is used). Though returning the max timestamp for each rid, the pids are not the pids for the timestamps from the original table. What query would give me the results I'm looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果你想避免 JOIN,你可以使用:
If you want to avoid a JOIN, you can use:
尝试:
Try:
(在 PostgreSQL 9.something 中测试)
识别 Rid 和时间戳。
加入它。
(Tested in PostgreSQL 9.something)
Identify the rid and timestamp.
Join to it.
希望我有帮助!
Hope I helped !
我在rid和时间戳上创建了一个索引。
显示第 0 - 2 行(总共 3 行,查询花费了 0.0104 秒)
此方法将从
theTable
(测试)中选择所有所需的值,在所有高于测试时间戳的时间戳上左连接自身 (maxt)与同样的摆脱。当时间戳已经是测试中最高的时间戳时,maxt 上没有匹配项 - 这就是我们正在寻找的 - maxt 上的值变为 NULL。现在我们使用 WHERE 子句maxt.rid IS NULL
或 maxt 上的任何其他列。I created an index on rid and timestamp.
Showing rows 0 - 2 (3 total, Query took 0.0104 sec)
This method will select all the desired values from
theTable
(test), left joining itself (maxt) on all timestamps higher than the one on test with the same rid. When the timestamp is already the highest one on test there are no matches on maxt - which is what we are looking for - values on maxt become NULL. Now we use the WHERE clausemaxt.rid IS NULL
or any other column on maxt.您也可以有这样的子查询:
但是这样,如果您希望将
cost
包含在显示的列中,等等,您将需要一个子查询。因此,
group by
和join
是更好的解决方案。You could also have subqueries like that:
But this way, you'll need one more subquery if you want
cost
included in the shown columns, etc.So, the
group by
andjoin
is better solution.