从表中获取最新数据然后进行总结

时间:2014-04-26 20:51:03

标签: mysql sql

我有一个记录某些应用程序数据的表。

它的结构类似于

CREATE TABLE `log` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `website_id` int(11) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `date` date DEFAULT NULL,
  `pages` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

数据

INSERT INTO `log` (`id`, `website_id`, `created_at`, `date`, `pages`)
VALUES
    (1,1,'2014-04-03 00:30:01','2014-04-03',30),
    (2,2,'2014-04-03 12:21:54','2014-04-03',13),
    (3,1,'2014-04-04 01:10:12','2014-04-04',40),
    (4,2,'2014-04-04 01:11:53','2014-04-04',20),
    (5,1,'2014-04-04 02:43:31','2014-04-04',5),
    (6,1,'2014-04-05 12:29:48','2014-04-05',8),
    (7,1,'2014-04-05 13:48:51','2014-04-05',0),
    (8,2,'2014-04-05 14:01:26','2014-04-05',20),
    (9,2,'2014-04-05 15:51:01','2014-04-05',30),
    (10,3,'2014-04-05 17:29:30','2014-04-05',15);

我想要做的是为每个网站提取当天的最新记录,然后对其进行总结,以便我得到网页列的整体计数。

所以期待3行

2014-04-03 | 43
2014-04-04 | 25
2014-04-05 | 45

因此,当获取网站的行时,需要先获取最新创建的行,所以这里

(6,1,'2014-04-05 12:29:48','2014-04-05',8),
(7,1,'2014-04-05 13:48:51','2014-04-05',0),

将使用id 7,因为它更新。 因此,2014-04-05网站ID 1的页数为0,而不是8

select date, sum(pages) from log group by date

结果

2014-04-03 | 43
2014-04-04 | 65
2014-04-05 | 73

它的出路,因为它没有过滤,但我不知道如何过滤它。

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT L1.`date`, SUM(L1.`pages`) `pages`
FROM `log` L1
JOIN
(
  SELECT `website_id`, MAX(`created_at`) as `created_at`
  FROM `log`
  GROUP BY `website_id`, `date`
) L2
ON L1.`website_id` = L2.`website_id`
  AND L1.`created_at` = L2.`created_at`
GROUP BY `date`

<强> SQL FIDDLE DEMO

答案 1 :(得分:0)

在MySQL中,使用not exists查找最新记录通常最快。以下内容最适合log(website_id, date, created_at)的索引:

select l.date, sum(l.pages)
from log l
where not exists (select 1
                  from log l2
                  where l2.website_id = l.website_id and
                        l2.date = l.date and
                        l2.created_at > l.created_at
                 )
group by l.date;