4

我想检索所有具有最近修改时间的记录。例如,以这张表为例:

|page_url|last_modified|
---------
|abc.com |2010-10-01 10:00:00|
|xyz.com |2010-10-04 12:10:00|
|xyz.com |2010-10-04 12:00:00|
|xyz.com |2010-10-04 10:00:00|
|abc.com |2010-10-01 08:00:00|

输出应包含以下数据:

|page_url|last_modified|
---------
|abc.com |2010-10-01 10:00:00|
|xyz.com |2010-10-04 12:10:00|

我曾尝试使用像下面这样的有子句,但对我不起作用:(

SELECT page_url, last_modified 
FROM
my_table
HAVING MAX(last_modified);

编辑 1:我的表中有 25 个字段可供使用。但是,我想我不能为所有人申请 group by。现在做什么?

4

5 回答 5

4

无需HAVING在此使用,仅GROUP BY从句。

SELECT page_url, MAX(last_modified) 
FROM my_table
GROUP BY page_url

更新 1

SELECT  a.*
FROM    my_table a
        INNER JOIN
        (
            SELECT page_url, MAX(last_modified) recentDate
            FROM my_table
            GROUP BY page_url
        ) b ON a.page_url = b.page_url AND
                a.last_modified = b.recentDate
于 2012-10-11T08:33:27.160 回答
2

尝试使用group byand max()inselect语句

SELECT page_url, max(last_modified) 
FROM  my_table
group by page_url

更多信息:

于 2012-10-11T08:34:06.987 回答
2

如果您使用的是具有元组功能的 RDBMS,IN例如 Postgresql、MySql 和 Oracle。您可以使用以下方法。

因此,假设您已经拥有这样的现有视图:

create view page_latest as

SELECT page_url, MAX(last_modified) recentDate
FROM tbl
GROUP BY page_url;


select * from page_latest;

输出...

| PAGE_URL |                     RECENTDATE |
---------------------------------------------
|  abc.com | October, 01 2010 10:00:00+0000 |
|  xyz.com | October, 04 2010 12:10:00+0000 |

...基于此数据:

| PAGE_URL |                  LAST_MODIFIED |            MESSAGE |
------------------------------------------------------------------
|  abc.com | October, 01 2010 10:00:00+0000 |           no alarm |
|  xyz.com | October, 04 2010 12:10:00+0000 |       no surprises |
|  xyz.com | October, 04 2010 12:00:00+0000 | fake plastic trees |
|  xyz.com | October, 04 2010 10:00:00+0000 |              creep |
|  abc.com | October, 01 2010 08:00:00+0000 |         thom yorke |

您可以通过重新使用上述视图和上述 RDBMS 的元组功能 IN 来找到所有最新消息:http ://www.sqlfiddle.com/#!2/b8193/2

select * 
from tbl
where (page_url,last_modified) in (select page_url, recentDate from page_latest);

输出:

| PAGE_URL |                  LAST_MODIFIED |      MESSAGE |
------------------------------------------------------------
|  abc.com | October, 01 2010 10:00:00+0000 |     no alarm |
|  xyz.com | October, 04 2010 12:10:00+0000 | no surprises |

不仅查询更短,而且更易于阅读。虽然如果您没有视图,您可以在查询中内联“视图”:http ://www.sqlfiddle.com/#!2/b8193/5

select * 
from tbl
where (page_url,last_modified) in

  (SELECT page_url, MAX(last_modified) recentDate
   FROM tbl
   GROUP BY page_url);

此答案在 Sql Server 中不起作用。只有少数数据库不支持 tuple-capable IN,不幸的是 Sql Server 就是其中之一

于 2012-10-11T13:43:13.747 回答
0
SELECT z.page_url,z.last_modified 
FROM (SELECT page_url, MAX(last_modified) 
      FROM my_table 
      GROUP BY page_url) as z 
ORDER BY z.page_url
于 2012-10-11T08:37:23.587 回答
0

这些怎么样-

    SELECT   page_url,last_modified,other field
    FROM my_table
    ORDER BY page_url,last_modified

或者

   SELECT  distinct page_url,last_modified,other field
   FROM my_table
   ORDER BY page_url,last_modified
于 2012-10-11T10:12:32.923 回答