根據您的回覆,您的查詢應該是:
// 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步方法。第一個是獲得前四個類別。第二個查詢是獲得這4個類別的子類別。
我不太贊成子查詢 - 我儘可能避免它們,所以在我看來,這不是一個非常有效的解決方案。你可以有效地從那裏穿過第一個查詢並獲得IDS像這樣:
// 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
貸Lion
什麼乾脆直接使用'LIMIT'子句中的SQL? – Lion
這是QUERY我不確定哪裏會去 $ categories_query =「select c.categories_id,cd.categories_name,c.parent_id from」。 TABLE_CATEGORIES。 「 C, 」 。 TABLE_CATEGORIES_DESCRIPTION。 「cd 其中c.categories_id = cd.categories_id 和c.categories_status = 1」。 「和cd.language_id ='」。 (int)$ _ SESSION ['languages_id']。 「' order by c.parent_id,c.sort_order,cd.categories_name」; $ categories = $ db-> Execute($ categories_query); –
如果您只需要結果集中的前四行查詢返回,則可以在'order by'子句後面(如果使用的是MySql)在SQL查詢中追加'limit 4'。 – Lion