简体   繁体   English

MySQL / PHP:如何按组列出结果,但限制所需的数据库查询

[英]MySQL/PHP: How to list results in groups but limit the DB queries required

I have a database as follows: 我有一个数据库,如下所示:

---------------------------------------------------------------
|       module_name      |     category    |       content    |
---------------------------------------------------------------
|       module1          |     category1   |       content    |
|       module2          |     category1   |       content    |
|       module3          |     category2   |       content    |
|       module4          |     category3   |       content    |
|       module5          |     category2   |       content    |
---------------------------------------------------------------

I want to be able to create groups of the results but cannot figure out the quickest and most efficient way to do so without nesting MySQL queries. 我希望能够创建结果的分组,但是如果不嵌套MySQL查询,就无法找出最快,最有效的方法。 The result should look like (Styling ignored): 结果应类似于(忽略样式):

---------------------------------------------------------------
|       module1         |     category1    |       content    |
|       module2         |     category1    |       content    |
---------------------------------------------------------------

---------------------------------------------------------------
|       module3         |     category2    |       content    |
|       module5         |     category2    |       content    |
---------------------------------------------------------------

---------------------------------------------------------------
|       module4         |     category3    |       content    |
---------------------------------------------------------------

My code looks like: 我的代码如下:

$query  = 'SELECT'
        . ' DISTINCT'
        . ' category AS name'
        . ' FROM #__table'
        . ' WHERE enabled = 1';
$db->setQuery($query);
$categories = $db->loadObjectList();
foreach ($categories as $category) {
$query  = 'SELECT'
    . ' module_name'
    . ' FROM #__table'
        . ' WHERE enabled = 1 AND category = \'' . $category->name . '\''
        . ' ORDER BY id';                       
    $db->setQuery($query);
    $results = $db->loadObjectList();
foreach ($results as $result) {
        echo $result->module_name;
    }
}

This is a nested query that works, but is there a better way to do this? 这是一个有效的嵌套查询,但是有更好的方法吗?

  1. Sort your recordset by the category column (then by module_name). 按类别列对记录集排序(然后按module_name排序)。
  2. Loop through your recordset, keeping track of the category you're in ($current_category). 遍历记录集,跟踪您所在的类别($ current_category)。
  3. When the row's category doesn't match $current_category, you start a new grouping. 当该行的类别与$ current_category不匹配时,您将开始一个新的分组。

More specifically, adapting your code: 更具体地说,调整您的代码:

$query  = 'SELECT'
        . ' module_name, category'
        . ' FROM #__table'
        . ' WHERE enabled = 1 '
        . ' ORDER BY category, id';                                               
$db->setQuery($query);
$results = $db->loadObjectList();

$current_category = null;
foreach ($results as $result) 
{
    if ($current_category != $result->category)
    {
        // whatever you do to separate the category listings
        //  if you don't do it before the first, check ($current_category != null)

        $current_category = $result->category
    }
    echo $result->module_name;
}

One approach would be to query the database for all rows sorted by category, and then do the grouping into separate tables in PHP. 一种方法是在数据库中查询按类别排序的所有行,然后在PHP中将其分组为单独的表。 You could either iterate over the rows keeping track of changes in category and output to HTML directly, or else build a structure of nested arrays that reflect the structure of the groups and the rows within each group and then use that to render your HTML. 您可以遍历行,以跟踪类别的变化并直接输出到HTML,或者构建嵌套数组的结构以反映组的结构以及每个组中的行,然后使用它来呈现HTML。

To me it looks like your laying out your datascheme in 3 tables and not 1 table with 3 columsn and multiple rows. 对我来说,这看起来像是将数据方案布置在3个表中,而不是在3个列和多行的1个表中进行布局。 In this scenariao: 在这个场景中:

Join your tables and group your results on category. 加入表并将结果分类。

Just use an array. 只需使用一个数组。 Unless your data set is ginormous. 除非您的数据集是巨大的。

foreach($results as $result)
    //assuming $result is an associative array
    $sorted_results[$result['category']][] = $result;
}

//now you can do fun stuff like
foreach($sorted_results as $category => $category_result_list) {
    //do something meaningful
}

Edit: Just saw your code sample 编辑:刚刚看到您的代码示例

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

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