从相似行组中选择每分钟(日期)?

时间:2012-09-11 03:57:57

标签: mysql

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

Verb | Object | Date

我按照Verb和Object分组,将所有相似的行分组,这是我当前的查询:

SELECT * FROM Posts GROUP BY Verb, Object

我需要知道每组中最早行的日期。怎么办呢?

2 个答案:

答案 0 :(得分:1)

怎么样

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

查看GROUP BY (Aggregate) Functions

答案 1 :(得分: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`;