我有一张桌满了来自不同来源的项目。一些来源可能具有相同的位置(在我的示例中,不同的BBC新闻提要将是不同的来源,但它们全部来自BBC)。每个项目都有一个“唯一”ID,可用于在同一位置识别其他项目。这意味着与网站上同一个新闻故事相关的项目,但在不同的Feed中发布的项目将具有相同的“唯一ID”,但这不一定是全球唯一的。以优先顺序聚合SQL行
问题是我想在显示时间消除重复项,以便(取决于您看到的是哪些Feed)只能获得每个故事的最多一个版本,即使两个或三个供稿可能包含链接到它。
我有一个sources
表与关于每个来源的信息,location_id
和location_precedence
字段。然后我有一个包含每个项目的items
表,它的unique_id
,source_id
和content
。具有相同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)。
你可以为了通过不包含在GROUP BY列的location_priority“的文章? – 2009-12-06 13:06:12
@Yonatan Karni:在MySQL中,你可以。它的行为就像一个'any()'聚合函数:) – Andomar 2009-12-06 13:32:55
另请参见: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