从 M 组中选择前 N 行
我有这个表:
CREATE TABLE IF NOT EXISTS `catalog_sites` (
`id` int(10) unsigned NOT NULL auto_increment,
`cat_id` int(10) unsigned NOT NULL,
`date` datetime NOT NULL,
`url` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`keywords` varchar(255) NOT NULL,
`visited` int(10) unsigned NOT NULL,
`shown` int(10) unsigned NOT NULL,
`meta_try` int(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
我认为我的问题很简单,但似乎找不到合适的解决方案。
所以,这是一个包含网站的表,我想获得 6 个不同类别的 6 个网站(cat_id,总计:36行),每个类别的评分最高。评级的计算方式为访问
/ 显示
。
我应该得到 36 行,其中包含 6 个顶级类别(我们可以通过使用 AVG(visited / shown)
排序来找到它们),以及这 6 个类别中每个类别的 6 个顶级网站。
如果您有任何想法如何以不同的方式发生这种情况,请告诉我。
I have this table:
CREATE TABLE IF NOT EXISTS `catalog_sites` (
`id` int(10) unsigned NOT NULL auto_increment,
`cat_id` int(10) unsigned NOT NULL,
`date` datetime NOT NULL,
`url` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,
`keywords` varchar(255) NOT NULL,
`visited` int(10) unsigned NOT NULL,
`shown` int(10) unsigned NOT NULL,
`meta_try` int(1) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `url` (`url`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
I think my problem is simple, but cant seem to find an appropriate solution..
So, this is a table with web-sites, I would like to get 6 sites in 6 different categories (cat_id, total: 36 rows) with the highest rating for each category. The rating is calculated as visited
/ shown
.
I should get 36 rows containing 6 top categories (we can find them by sorting with AVG(visited / shown)
), and 6 top sites in each of these 6 categories.
If you have any ideas how this might happen differently, please tell me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该可以让你使用 MySQL 变量得到你想要的,内部查询将预先计算访问/显示的排名,并使用你想要的条件的排序...每个类别,最高排名...然后使用@vars 将按顺序保持 @RankSeq 1-?每个类别。从该 Prequery(别名 PQ)中,OUTER 查询仅查询 URL 排名序列 <= 6 的 PreQuery
为了进一步确保您只获得前 6 个类别,内部 PreQuery 还具有预查询/限制“热门类别”(别名)
This should get you what you want using MySQL Variables, the inner query will pre-calculate the rank of visited / shown, and using an order by by the condition you want... Per Category, the highest ranks... and then using @vars will keep the @RankSeq sequentially 1-? per category. From that Prequery (aliased PQ), the OUTER query just queries the PreQuery where the URL's Rank Sequence is <= 6
To further ensure you are only getting the top 6 categories, the inner PreQuery also has a pre-query / limit for the "TopCategories" (alias)
我已经尝试过你的例子,但它对我来说并不真正有用,或者我只是不知道如何使其适应我的情况。无论如何,就 SQL 而言我仍然是菜鸟,所以我无法理解你的查询。
不过,我已经设法解决了我的问题。这很复杂,而且可能是最糟糕的方法。它也很慢,但我会缓存结果,所以这应该不是问题。
这是我的解决方案:
I've tried your example, but it doesn't really work for me, or I just don't know how to adapt it to my case. Anyway, I'm still a noob as far as SQL goes, so I couldn't understand your query.
I have managed to solve my problem however. It's complicated and probably the worst possible approach. It is slow too, but I'll cache the results, so that shouldn't be a problem.
Here is my solution: