0

这是表Posts结构(还有其他列对问题不重要):

Verb | Object | Date

我按动词分组,然后按对象分组,对所有相似的行进行分组,这是我当前的查询:

SELECT * FROM Posts GROUP BY Verb, Object

我需要知道每组中最旧行的日期。如何才能做到这一点?

4

2 回答 2

1

怎么样

SELECT  Verb, 
        Object, 
        MIN(date) OldestDate
FROM    Posts
GROUP BY    Verb, Object

看看GROUP BY(聚合)函数

于 2012-09-11T04:01:05.033 回答
0

要获取每个组中最早的日期,请尝试以下操作:

SELECT Verb, Object, MIN(`date`) AS min_date
FROM Posts
GROUP BY Verb, Object;

如果要获取每个组中日期最早的记录,请尝试以下操作:

然后重启 MySQL 服务器。

SELECT a.*
FROM Posts a
     INNER JOIN (SELECT Verb, Object, MIN(`Date`) AS `Date`
                 FROM Posts
                 GROUP BY Verb, Object
                ) b
                ON a.Verb =  b.Verb
                   AND a.Object = b.Object
                   AND a.`Date` = b.`Date`;
于 2012-09-11T04:01:45.627 回答