MySQL查询,MAX()+ GROUP BY

时间:2011-04-14 01:02:09

标签: mysql sql database relational-database

Daft SQL问题。我有一个像这样的表('pid'是自动增加主要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;

实际表格数据:

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 1是第一次使用rid 1,行/ pid 3是第一次使用RID 2,row / pid 7是第一次摆脱使用3)。虽然返回每个rid的最大时间戳,但pids不是原始表中时间戳的pid。什么查询会给我我正在寻找的结果?

7 个答案:

答案 0 :(得分:51)

(在PostgreSQL 9.something中测试过)

识别rid和timestamp。

select rid, max(timestamp) as ts
from test
group by rid;

1   2011-04-14 18:46:00
2   2011-04-14 14:59:00

加入它。

select test.pid, test.cost, test.timestamp, test.rid
from test
inner join 
    (select rid, max(timestamp) as ts
    from test
    group by rid) maxt
on (test.rid = maxt.rid and test.timestamp = maxt.ts)

答案 1 :(得分:7)

select *
from (
    select `pid`, `timestamp`, `cost`, `rid`
    from theTable 
    order by `timestamp` desc
) as mynewtable
group by mynewtable.`rid`
order by mynewtable.`timestamp`

希望我帮忙!

答案 2 :(得分:4)

SELECT t.pid, t.cost, to.timestamp, t.rid
FROM test as t
JOIN (
    SELECT rid, max(tempstamp) AS maxtimestamp
    FROM test GROUP BY rid
) AS tmax
    ON t.pid = tmax.pid and t.timestamp = tmax.maxtimestamp

答案 3 :(得分:2)

我在rid和timestamp上创建了一个索引。

SELECT test.pid, test.cost, test.timestamp, test.rid
FROM theTable AS test
LEFT JOIN theTable maxt 
ON maxt.rid = test.rid
AND maxt.timestamp > test.timestamp
WHERE maxt.rid IS NULL 

显示0到2行(总计3行,查询耗时0.0104秒)

此方法将从theTable(测试)中选择所有所需的值,在所有时间戳上保持连接自身(maxt)高于具有相同rid的测试中的时间戳。当时间戳已经是测试中的最高时间戳时,maxt上没有匹配 - 这正是我们要寻找的 - maxt上的值变为NULL。现在我们在maxt上使用WHERE子句maxt.rid IS NULL或任何其他列。

答案 4 :(得分:0)

你也可以有这样的子查询:

SELECT ( SELECT MIN(t2.pid)
         FROM test t2
         WHERE t2.rid = t.rid
           AND t2.timestamp = maxtimestamp
       ) AS pid 
     , MAX(t.timestamp) AS maxtimestamp
     , t.rid
FROM test t
GROUP BY t.rid

但是这样,如果您希望在显示的列中包含cost等,则还需要一个子查询。

因此,group byjoin是更好的解决方案。

答案 5 :(得分:0)

如果您想避免加入,可以使用:

SELECT pid, rid FROM theTable t1 WHERE t1.pid IN ( SELECT MAX(t2.pid) FROM theTable t2 GROUP BY t2.rid);

答案 6 :(得分:-1)

尝试:

select pid,cost, timestamp, rid from theTable order by timestamp DESC limit 2;