1

我试图在数据库查询上放置一个限制子句,只显示前 4 个结果。

我正在使用的当前代码是

    $categories_query = "select c.categories_id, cd.categories_name, c.parent_id
                      from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
                       where c.categories_id = cd.categories_id
                       and c.categories_status=1 " .
//                             "and c.categories_id = ptc.category_id " .
//                             "and ptc.category_id = cd.categories_id " .
//                             "and ptc.product_type_id not in  " . $this->document_types_list . "
                       " and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                         order by c.parent_id, c.sort_order, cd.categories_name";
         $categories = $db->Execute($categories_query);
         while (!$categories->EOF) {
           $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => 0);
           $categories->MoveNext();
         }

我遇到的问题是我找到了一种通过LIMIT 4在订单查询之后放置仅限制 4 个结果的方法,它显示了前 4 个父类别,但它也从列表中删除了子类别。

所以我想拿这个

<ul>
   <li>Menu One</li>
   <li>Menu Two
      <ul>
         <li>Submenu One</li>
         <li>Submenu Two</li>
         <li>Submenu Three</li>
      </ul>
   </li>
   <li>Menu Three</li>
   <li>Menu Four</li>
   <li>Menu Five</li>
   <li>ect...</li>
</ul>

看到这个

<ul>
   <li>Menu One</li>
   <li>Menu Two
      <ul>
         <li>Submenu One</li>
         <li>Submenu Two</li>
         <li>Submenu Three</li>
      </ul>
   </li>
   <li>Menu Three</li>
   <li>Menu Four</li>
</ul>

并且在查询中放置“LIMIT 4”时,它只显示这个

<ul>
   <li>Menu One</li>
   <li>Menu Two</li>
   <li>Menu Three</li>
   <li>Menu Four</li>
</ul>

如何将子类别与父类别保持一致?Aparently 仅调用前 4 个不会调用其下的类别

4

1 回答 1

0

该查询完全按照您的要求执行 - “LIMIT”表示您仅返回 4 条记录,按 categoryID 排序。

为了得到你想要的,你必须改写查询 - 我没有在这台笔记本电脑上安装 SQL,所以很难测试,但是像这样:

$categories_query = "select c.categories_id, cd.categories_name, c.parent_id
                      from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd 
                       where c.categories_id = cd.categories_id
                       and c.categories_status=1 " .
                       " and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                       " and c.parent_id in " .
                       " ( select parent_id from " . TABLE_CATEGORIES . " limit 4) " .
                       "  order by c.parent_id, c.sort_order, cd.categories_name";
         $categories = $db->Execute($categories_query);
         while (!$categories->EOF) {
           $this->data[$categories->fields['parent_id']][$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'], 'count' => 0);
           $categories->MoveNext();
         }
于 2012-09-24T21:22:38.837 回答