2009-12-06 51 views
0

我有一张桌满了来自不同来源的项目。一些来源可能具有相同的位置(在我的示例中,不同的BBC新闻提要将是不同的来源,但它们全部来自BBC)。每个项目都有一个“唯一”ID,可用于在同一位置识别其他项目。这意味着与网站上同一个新闻故事相关的项目,但在不同的Feed中发布的项目将具有相同的“唯一ID”,但这不一定是全球唯一的。以优先顺序聚合SQL行

问题是我想在显示时间消除重复项,以便(取决于您看到的是哪些Feed)只能获得每个故事的最多一个版本,即使两个或三个供稿可能包含链接到它。

我有一个sources表与关于每个来源的信息,location_idlocation_precedence字段。然后我有一个包含每个项目的items表,它的unique_id,source_idcontent。具有相同unique_id和来源location_id的项目应该最多显示一次,最高来源location_precedence获胜。

我本来以为是这样的:

SELECT `sources`.`name` AS `source`, 
     `items`.`content`, 
     `items`.`published` 
FROM `items` INNER JOIN `sources` 
    ON `items`.`source_id` = `sources`.`id` AND `sources`.`active` = 1 
GROUP BY `items`.`unique_id`, `sources`.`location_id` 
ORDER BY `sources`.`location_priority` DESC 

会做的伎俩,但似乎忽略了位置优先级字段。我错过了什么?


示例数据:

CREATE TABLE IF NOT EXISTS `sources` (
    `id` int(10) unsigned NOT NULL auto_increment, 
    `location_id` int(10) unsigned NOT NULL, 
    `location_priority` int(11) NOT NULL, 
    `active` tinyint(1) unsigned NOT NULL default '1', 
    `name` varchar(150) NOT NULL, 
    `url` text NOT NULL, 
    PRIMARY KEY (`id`), 
    KEY `active` (`active`) 
); 

INSERT INTO `sources` (`id`, `location_id`, `location_priority`, `active`, `name`, `url`) VALUES 
(1, 1, 25, 1, 'BBC News Front Page', 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml'), 
(2, 1, 10, 1, 'BBC News England', 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/england/rss.xml'), 
(3, 1, 15, 1, 'BBC Technology News', 'http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/technology/rss.xml'), 
(4, 2, 0, 1, 'Slashdot', 'http://rss.slashdot.org/Slashdot/slashdot'), 
(5, 3, 0, 1, 'The Daily WTF', 'http://syndication.thedailywtf.com/TheDailyWtf'); 

CREATE TABLE IF NOT EXISTS `items` (
    `id` bigint(20) unsigned NOT NULL auto_increment, 
    `source_id` int(10) unsigned NOT NULL, 
    `published` datetime NOT NULL, 
    `content` text NOT NULL, 
    `unique_id` varchar(255) NOT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `unique_id` (`unique_id`,`source_id`), 
    KEY `published` (`published`), 
    KEY `source_id` (`source_id`) 
); 

INSERT INTO `items` (`id`, `source_id`, `published`, `content`, `unique_id`) VALUES 
(1, 1, '2009-12-01 16:25:53', 'Story about Subject One',      'abc'), 
(2, 2, '2009-12-01 16:21:31', 'Subject One in story',      'abc'), 
(3, 3, '2009-12-01 16:17:20', 'Techy goodness',        'def'), 
(4, 2, '2009-12-01 16:05:57', 'Further updates on Foo case',     'ghi'), 
(5, 3, '2009-12-01 15:53:39', 'Foo, Bar and Quux in court battle',   'ghi'), 
(6, 2, '2009-12-01 15:52:02', 'Anti-Fubar protests cause disquiet',   'mno'), 
(7, 4, '2009-12-01 15:39:00', 'Microsoft Bleh meets lukewarm reception',  'pqr'), 
(8, 5, '2009-12-01 15:13:45', 'Ever thought about doing it in VB?',   'pqr'), 
(9, 1, '2009-12-01 15:13:15', 'Celebrity has 'new friend'',  'pqr'), 
(10, 1, '2009-12-01 15:09:57', 'Microsoft launches Bleh worldwide',   'stu'), 
(11, 2, '2009-12-01 14:57:22', 'Microsoft launches Bleh in UK',    'stu'), 
(12, 3, '2009-12-01 14:57:22', 'Microsoft launches Bleh',      'stu'), 
(13, 3, '2009-12-01 14:42:15', 'Tech round-up',        'vwx'), 
(14, 2, '2009-12-01 14:36:26', 'Estates 'old news' say government', 'yza'), 
(15, 1, '2009-12-01 14:15:21', 'Iranian doctor 'was poisoned'',  'bcd'), 
(16, 4, '2009-12-01 14:14:02', 'Apple fans overjoyed by iBlah',    'axf'); 

查询后所期望的内容:

  • 故事有关主题的一个
  • 易怒善
  • 富,酒吧和QUUX在法庭斗争
  • 反富足r抗议造成不安
  • 微软Bleh遇见温柔接待
  • 曾经想过在VB中做这件事吗?
  • 名人有“新朋友”
  • 微软推出的Bleh全球
  • 技术围捕
  • 庄园“老新闻”说,政府
  • 伊朗医生是被毒死的“
  • 苹果迷们喜出望外通过iBlah

我试着通过Andomar解决方案的变化,一些成功:

SELECT  s.`name` AS `source`, 
      i.`content`, 
      i.`published` 
FROM  `items` i 
INNER JOIN `sources` s 
ON   i.`source_id` = s.`id` 
AND   s.`active` = 1 
INNER JOIN (
    SELECT `unique_id`, `source_id`, MAX(`location_priority`) AS `prio` 
    FROM `items` i 
    INNER JOIN `sources` s ON s.`id` = i.`source_id` AND s.`active` = 1 
    GROUP BY `location_id`, `unique_id` 
) `filter` 
ON   i.`unique_id` = `filter`.`unique_id` 
AND   s.`location_priority` = `filter`.`prio` 
ORDER BY i.`published` DESC 
LIMIT 50 

随着AND s.location_priority = filter.prio东西几乎工作,因为我想。因为一个项目可以来自多个来源具有相同的优先级,项目可以重复。在这种情况下,外部查询需要额外的GROUP BY i.unique_id来完成这项工作,如果优先级相同,我认为哪个源“胜出”并不重要。

我曾试过用AND i.source_id = filter.source_id代替,它几乎可以工作(即消除了额外的GROUP BY),但没有给出正确来源的结果。在上面的例子中,它给了我“Foo case的进一步更新”(来源于“BBC News England”),而不是“Foo,Bar and Quux在法庭上的战斗”(来源于“BBC技术新闻”)。查询时,我得到:

unique_id: 'ghi' 
source_id: 2 
prio: 15 

注意源ID是不正确的(预期:3)。

+0

你可以为了通过不包含在GROUP BY列的location_priority“的文章? – 2009-12-06 13:06:12

+0

@Yonatan Karni:在MySQL中,你可以。它的行为就像一个'any()'聚合函数:) – Andomar 2009-12-06 13:32:55

+0

另请参见:http://stackoverflow.com/questions/1438978/sql-query-to-get-max-value-based-on-different-max- value-given-multiple-records,http://stackoverflow.com/questions/95866/select-max-in-group,http://stackoverflow.com/questions/1299556/sql-group-by-max,http: //stackoverflow.com/questions/1305056/mysql-selecting-all-corresponding-fields-using-max-and-group-by,http://stackoverflow.com/questions/526143/group-by-max,http: //stackoverflow.com/questions/1339624/sql-select-unique-rows-from-a-group-of-results,可能还有其他人。 – outis 2009-12-06 14:16:02

回答

5

Order by只是命令行,它并不挑选其中。

其中一个过滤掉的行具有较低的方法是使用一个inner join作为过滤器:

SELECT  s.name, i.content, i.published 
FROM  items i 
INNER JOIN sources s 
ON   i.source_id = s.id 
AND  s.active = 1 
INNER JOIN (
    SELECT unique_id, max(location_priority) as prio 
    FROM items i 
    INNER JOIN sources s ON s.id = i.source_id AND s.active = 1 
    GROUP BY unique_id) filter 
ON   i.unique_id = filter.unique_id 
AND  s.location_priority = filter.prio; 

一种替代方案是一个where ... in <subquery>子句,例如:

SELECT  s.name, i.content, i.published 
FROM  items i 
INNER JOIN sources s 
ON   i.source_id = s.id 
AND  s.active = 1 
WHERE  (i.unique_id, s.location_priority) IN (
    SELECT unique_id, max(location_priority) 
    FROM items i 
    INNER JOIN sources s ON s.id = i.source_id AND s.active = 1 
    GROUP BY unique_id 
); 

此问题也被称为“选择保持组范围最大值的记录”。 Quassnoi已经写了nice article

编辑:一个方法来打破与多个来源在同一优先级的关系是WHERE子句与子查询。这个例子破坏上i.id DESC关系:

SELECT  s.name, i.unique_id, i.content, i.published 
FROM  (
      SELECT unique_id, min(location_priority) as prio 
      FROM items i 
      INNER JOIN sources s ON s.id = i.source_id AND s.active = 1 
      GROUP BY unique_id 
      ) filter 
JOIN  items i 
JOIN  sources s 
ON   s.id = i.source_id 
      AND s.active = 1 
WHERE  i.id = 
      (
      SELECT i.id 
      FROM  items i 
      JOIN  sources s 
      ON  s.id = i.source_id 
        AND s.active = 1 
      WHERE i.unique_id = filter.unique_id 
      AND  s.location_priority = filter.prio 
      ORDER BY i.id DESC 
      LIMIT 1 
      ) 

Quassnoi也有selecting records holding group-wise maximum (resolving ties) :)

+0

谢谢!文章(并知道如何描述问题)非常有用。 – DMI 2009-12-06 13:42:07

+0

另请参阅:http://dev.mysql.com/doc/refman/5.1/en/example-maximum-column-group-row.html – outis 2009-12-06 13:52:23

+0

Argh。所以我试过这个解决方案,但它似乎没有工作。我已经更新了主要帖子的细节。 – DMI 2009-12-06 22:30:16

1

做一个自我加盟派生表像

select max(location_priority) from table where ... 
0

什么都有我错过了吗?

ORDER BY发生在GROUP BY已经将每个组缩减为一行之后发生。保罗给出了一个决议。

至于与查询问题:

SELECT `unique_id`, `source_id`, MAX(`location_priority`) AS `prio` 
FROM `items` i 
INNER JOIN `sources` s ON s.`id` = i.`source_id` AND s.`active` = 1 
GROUP BY `location_id`, `unique_id` 

source_id既不聚集也不分组。因此,你得到的价值是不确定的。

+0

这不起作用:您不能在HAVING子句中使用非聚合列。即使可以,这也会隐藏所有具有非高优先级的不活动源的故事。 – Andomar 2009-12-06 13:29:08

+0

@Andormar:在MySQL中,你可以。加入确保从不考虑最高优先级的不活动源。真正的问题是,在GROUP BY之后显然有HAVING过滤减少了行数。 – outis 2009-12-06 13:44:19

+0

@outis:我认为你可以在SELECT中使用它们,但是在'HAVING'中他们会给出'未知列'错误 – Andomar 2009-12-06 13:47:35