仅查询父/子类别中指定数量的项目

发布于 2024-09-01 18:49:28 字数 1649 浏览 1 评论 0原文

我无法弄清楚如何查询某个类别中的每个项目,并且仅按日期列出最新的 10 个项目。这是我的表格布局:

download_categories

category_id (int) primary key
title (var_char)
parent_id (int)</pre></code>

downloads

id (int) primary key
title (var_char)
category_id (int)
date (date)</pre></code>

我需要查询主类别中的每个文件,比如说,有 100 个项目和 5 个子类别,并且只吐出最后添加的 10 个。我现在有一些功能,只需将所有文件相加,这样我就可以按类别获取计数,但我似乎无法修改代码以仅根据日期显示一定数量的项目。

我修改了用于计算类别及其子级中所有文件的代码以列出文件。主要问题是当我向 SQL 添加 LIMIT 子句时,它仅限制从该类别列出的结果,而不是整个查询。

该代码将所有下载类别放入一个数组中,然后循环遍历每个类别,使用该category_id 查询下载。

我最好的选择是将结果放入数组中,然后从那里对其进行排序吗?

function list_cat_files($parent, $start) {

    global $menu_array;

    if($start == 1) {

        unset($GLOBALS["total"]);

        $query = mysql_query("SELECT * FROM download_categories");

        while ( $row = mysql_fetch_assoc($query) ) {

            $menu_array[$row['category_id']] =
              array(
                'name'        => $row['title'],
                'parent'      => $row['parent_id'],
                'category_id' => $row['category_id']);

        }
    }

    foreach($menu_array as $key => $value) {

        global $total;

        if ($value['parent'] == $parent) {

            $category_id = $value['category_id'];

            $query1 = mysql_query("SELECT lid AS id, title, date, category_id FROM downloads WHERE category_id='$category_id'");

            while($item = mysql_fetch_array($query1)) {

                $total .= "--- ($item[lid]) : $item[title] <BR><BR>";
            }

            list_cat_files($key, 0);
        }
    }
    return $total;
}

I'm having trouble figuring out how to query every item in a certain category and only list the newest 10 items by date. Here is my table layout:

download_categories

category_id (int) primary key
title (var_char)
parent_id (int)</pre></code>

downloads

id (int) primary key
title (var_char)
category_id (int)
date (date)</pre></code>

I need to query every file in a main category that let's, say, have 100 items and 5 child categories and only spit out the last 10 added. I have functions right now that just add up all the files so I can get a count by category, but I can't seem to modify the code to only display a certain amount of items based on the date.

I modified the code I am using to count all the files in a category and its children to list the files. The main problem is when I add a LIMIT clause to my SQL, it just limits the results listed from that category, not the overall query.

The code puts all the download categories in an array then loops through each one querying the downloads with that category_id.

Is my best bet is to put the results in an array and then sort it from there?

function list_cat_files($parent, $start) {

    global $menu_array;

    if($start == 1) {

        unset($GLOBALS["total"]);

        $query = mysql_query("SELECT * FROM download_categories");

        while ( $row = mysql_fetch_assoc($query) ) {

            $menu_array[$row['category_id']] =
              array(
                'name'        => $row['title'],
                'parent'      => $row['parent_id'],
                'category_id' => $row['category_id']);

        }
    }

    foreach($menu_array as $key => $value) {

        global $total;

        if ($value['parent'] == $parent) {

            $category_id = $value['category_id'];

            $query1 = mysql_query("SELECT lid AS id, title, date, category_id FROM downloads WHERE category_id='$category_id'");

            while($item = mysql_fetch_array($query1)) {

                $total .= "--- ($item[lid]) : $item[title] <BR><BR>";
            }

            list_cat_files($key, 0);
        }
    }
    return $total;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小忆控 2024-09-08 18:49:28

这样的事情会有帮助吗?假设 $categories 是您想要的类别 id 的数组。

$query = FALSE;


foreach ($categories as $category){
    $query .= $query ? ' UNION ': '';

    $query .= "SELECT * from `downloads`
        WHERE category_id=$category order by date limit 10\n";

}

// run query here

如果有帮助的话,当我处理复杂的类别结构时,我是这样处理的。我会进行尽可能多的查询,以便作为开发人员轻松地进行排序。螺杆性能。然后,我根据需要缓存输出,并将缓存的文件用于公共使用。类别不会经常更改,因此这些资源匮乏的查询仅在管理员更改内容时偶尔运行,并且一次仅运行 1 个人,而不是所有访问者。

Would something like this help? Assuming $categories is an array of the category ids you want.

$query = FALSE;


foreach ($categories as $category){
    $query .= $query ? ' UNION ': '';

    $query .= "SELECT * from `downloads`
        WHERE category_id=$category order by date limit 10\n";

}

// run query here

If it helps, when I deal with complicated category structures, here's how I approach it. I do as many queries as I want to make sorting it out easy for me as the developer. Screw performance. Then I cache the output as needed and use the cached file for public consumption. Categories don't change that often, so those resource hungry queries are only run on occasion when an admin is changing things around, and only by 1 person at a time, rather than everyone who is visiting.

风启觞 2024-09-08 18:49:28
select * from downloads where category_id=X order by date limit 10

对每个类别重复此操作。除非你想实现嵌套集合/修改先序树遍历 (MPTT):http://mikehillyer. com/articles/managing-hierarchical-data-in-mysql/

select * from downloads where category_id=X order by date limit 10

Repeat for each category. Unless you want to implement a nested set/modified preorder tree traversal (MPTT): http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文