根据您的回复,您的查询应该是:
// Categories
$lid = (int) $_SESSION['languages_id'];
$sql = "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 = '{$lid}' "
. "ORDER BY c.parent_id, c.sort_order, cd.categories_name "
. "LIMIT 4";
$categories = $db->Execute($sql);
// Subcategories of the above
$lid = (int) $_SESSION['languages_id'];
$sql = "SELECT c.categories_id, cd.categories_name, c.parent_id "
. "FROM " . TABLE_CATEGORIES . " c, "
. TABLE_CATEGORIES_DESCRIPTION . " cd "
. "WHERE c.categories_id IN ("
. "SELECT c.categories_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 = '{$lid}' "
. "ORDER BY c.parent_id, c.sort_order, cd.categories_name "
. "LIMIT 4"
. ");
$subcategories = $db->Execute($sql);
我不知道你的数据库的结构,但你可以使用上面的方法来做一个两步的方法。第一个是获得前 4 个类别。第二个查询是获取这 4 个类别的子类别。
我不太赞成子查询——我尽可能避免使用它们,所以在我看来这不是一个非常有效的解决方案。您可以有效地遍历第一个查询并从那里获取 id,如下所示:
// Categories
$lid = (int) $_SESSION['languages_id'];
$sql = "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 = '{$lid}' "
. "ORDER BY c.parent_id, c.sort_order, cd.categories_name "
. "LIMIT 4";
$categories = $db->Execute($sql);
// Loop through the $categories and get the ids
$category_ids = array();
foreach ($categories as $category)
{
$category_ids[] = $caregory->categories_id;
}
// Subcategories of the above
$lid = (int) $_SESSION['languages_id'];
$sql = "SELECT c.categories_id, cd.categories_name, c.parent_id "
. "FROM " . TABLE_CATEGORIES . " c, "
. TABLE_CATEGORIES_DESCRIPTION . " cd "
. "WHERE c.categories_id IN (" . implode(",", $category_ids) . ")";
$subcategories = $db->Execute($sql);
在我看来,上述内容比子查询要好得多。
高温高压
狮子的学分