简体   繁体   English

高级SQL查询。 每个类别的前12名(MYSQL)

[英]Advanced SQL query. Top 12 from each category (MYSQL)

I have a MYSQL5 database and PHP 5. I need a query for a games websites index page that only selects the first 12 from each category of games. 我有一个MYSQL5数据库和PHP 5.我需要一个游戏网站索引页面的查询,只能从每个类别的游戏中选择前12个。 Here is what I have so far. 这是我到目前为止所拥有的。

$db->query("SELECT * FROM  `games` WHERE status = 'game_published'  AND `featured` =  '1' ORDER BY `category`");

The php code then groups games of the same category together and displays them. 然后,php代码将相同类别的游戏组合在一起并显示它们。 But yeah it doesn't limit the number of games from each category like I want. 但是,它并没有像我想要的那样限制每个类别的游戏数量。

Here is exactly what the structure of the table looks like: i49.tinypic.com/aysoll.png 这正是表的结构:i49.tinypic.com/aysoll.png

Here is a blog post which sounds like what I am trying to do: http://www.e-nformation.net/content/view/title/MySQL+Top+N+in+each+group+(group+inner+limit) But I can't make sense of it. 这是一篇博客文章,听起来像我想做的: http//www.e-nformation.net/content/view/title/MySQL+Top+N+in+each+group+(group+inner+limit )但我无法理解它。

Any help is appreciated. 任何帮助表示赞赏。

How about this? 这个怎么样?

SELECT * FROM (
    SELECT
       games.*,
       @rn := CASE WHEN @category=category THEN @rn + 1 ELSE 1 END AS rn,
       @category := category
    FROM games, (SELECT @rn := 0, @category := NULL) AS vars
    WHERE status = 'game_published' AND featured = '1'
    ORDER BY category
) AS T1
WHERE rn <= 12

you could use UNION , if we are not talking about million of types... 如果我们不谈论数百万种类型,你可以使用UNION ......

pseudoSQL: pseudoSQL:

(SELECT * FROM table WHERE condition AND category = 'action' ORDER BY id LIMIT 10)
UNION
(SELECT * FROM table WHERE condition AND category = 'action' ORDER BY id LIMIT 10)
UNION
(SELECT * FROM table WHERE condition AND category = 'action' ORDER BY id LIMIT 10)

If you have array of categories in your PHP/ASP, you can generate this union on the fly. 如果PHP / ASP中有类别数组,则可以动态生成此联合。

More: http://dev.mysql.com/doc/refman/5.0/en/union.html 更多: http//dev.mysql.com/doc/refman/5.0/en/union.html

EDIT: Here's probably most useful resource: http://www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/ 编辑:这可能是最有用的资源: http//www.xaprb.com/blog/2006/12/07/how-to-select-the-firstleastmax-row-per-group-in-sql/

Use it well ^^ 好好利用它^^

To use the technique from the posts you mention, you need a way to order the games. 要使用您提到的帖子中的技术,您需要一种订购游戏的方法。 They're using article date. 他们正在使用文章日期。 Then they select the number of older articles for that company, and say there can't be more than three. 然后他们选择该公司的旧文章数量,并说不能超过三个。

If your games table has an auto-increment column called id , you can select the top 10 games per category like: 如果您的游戏桌有一个名为id的自动增量列,您可以选择每个类别的前10个游戏,如:

SELECT   *
FROM     games g1
WHERE    status = 'game_published'  
         AND featured = '1' 
         AND 10 > 
         (
         SELECT   COUNT(*)
         FROM     games g2
         WHERE    g2.status = 'game_published'  
                  AND g2.featured = '1' 
                  AND g1.category = g2.category
                  AND g2.id > g1.id
         )

The where condition says that there can't be more than 10 rows with the same category and a higher ID. where条件表示不能有超过10行具有相同类别和更高ID。

There may be a more elegant solution, but you can just execute a query for each category. 可能有更优雅的解决方案,但您可以只为每个类别执行查询。 First get a list of categories: 首先得到一个类别列表:

SELECT DISTINCT(category) FROM  `games`;

Then take each of the results and query for 12 rows: 然后取每个结果并查询12行:

SELECT * FROM games WHERE status = 'game_published'  
AND `featured` =  '1' AND `category` = $category LIMIT 12;

Of course you need to add some kind of ranking row (and order by it) to get the top 12. 当然你需要添加一些排名行(并按顺序排列)来获得 12名。

Note : There may be a way to do this with a single query, but it escapes me at the moment. 注意 :可能有一种方法可以通过单个查询执行此操作,但此刻它让我感到惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM