返信に基づいて、クエリは次のようになります。
// 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);
あなたのデータベースの構造はわかりませんが、上記を使用して2段階のアプローチを行うことができます。最初の 1 つは、最初の 4 つのカテゴリを取得することです。2 番目のクエリは、これら 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);
私の見解では、上記はサブクエリよりもはるかに優れています。
HTH
ライオンへのクレジット