我已經寫了一個查詢,它返回的所有記錄都使用WHERE a.id =來正確顯示整個集合或單個文章的一些多對多連接。幫助限制加入的mysql數據庫查詢
SELECT a.id, date_added, title, content, category_id, person_id, organization_id, c.name AS category_name, firstname, lastname, o.name AS organization_name FROM articles AS a LEFT OUTER JOIN articles_categories AS ac ON a.id=ac.article_id LEFT OUTER JOIN categories AS c ON c.id=ac.category_id LEFT OUTER JOIN articles_people AS ap ON a.id=ap.article_id LEFT OUTER JOIN people AS p ON p.id=ap.person_id LEFT OUTER JOIN articles_organizations AS ao ON a.id=ao.article_id LEFT OUTER JOIN organizations AS o ON o.id=ao.organization_id ORDER BY date_added
但是!
我碰到了一堵磚牆,試圖找出如何將文章限制爲特定數量的ID,以便與分頁工作。
我理想地嘗試使用盡可能簡單和清晰的SQL語句,因爲我在其活動記錄類中使用了codeigniter框架。 http://codeigniter.com/user_guide/database/active_record.html
會很感激一些幫助,因爲我不希望恢復使用多個查詢這個,因爲我已經試圖減少下來到數據庫效率的單個查詢。
有四處搜尋,並嘗試了一些替代品,但似乎沒有任何工作。非常感謝!
比如我返回的結果都是這樣
--------------------------------------------------------------------- id title category_id person_id organization_id --------------------------------------------------------------------- 1 test 1 1 1 1 test 2 1 1 1 test 1 2 1 1 test 1 1 2 1 test 5 1 1 1 test 8 1 1 1 test 1 4 1 1 test 1 4 2 1 test 1 1 1 2 test 2 2 1 1 2 test 2 1 2 1 2 test 2 1 1 2 2 test 2 5 1 1 2 test 2 8 1 1 2 test 2 1 4 1 2 test 2 1 4 2
我需要的結果是這樣,這樣我可以在這樣的PHP創建子陣:
$articles = $query->result_array();
$output = array();
foreach ($articles as $article) {
// set up article details
$article_id = $article['id'];
// add article details
$output[$article_id]['article_id'] = $article_id;
$output[$article_id]['date_added'] = $article['date_added'];
$output[$article_id]['title'] = $article['title'];
$output[$article_id]['content'] = $article['content'];
// set up people details and add people array with details if exists
if (isset($article['person_id'])) {
$person_id = $article['person_id'];
$output[$article_id]['people'][$person_id]['person_id'] = $person_id;
$output[$article_id]['people'][$person_id]['lastname'] = $article['lastname'];
$output[$article_id]['people'][$person_id]['firstname'] = $article['firstname'];
}
// set up organizations details and add organizations array with details if exists
if (isset($article['organization_id'])) {
$organization_id = $article['organization_id'];
$output[$article_id]['organizations'][$organization_id]['organization_id'] = $organization_id;
$output[$article_id]['organizations'][$organization_id]['organization_name'] = $article['organization_name'];
}
// set up categories details and add categories array with details if exists
if (isset($article['category_id'])) {
$category_id = $article['category_id'];
$output[$article_id]['categories'][$category_id]['category_id'] = $category_id;
$output[$article_id]['categories'][$category_id]['category_name'] = $article['category_name'];
}
}
但是,如果我只需使用LIMIT(帶偏移等)1個
結果我得到的是
--------------------------------------------------------------------- id title category_id person_id organization_id --------------------------------------------------------------------- 1 test 1 1 1
代替
--------------------------------------------------------------------- id title category_id person_id organization_id --------------------------------------------------------------------- 1 test 1 1 1 1 test 2 1 1 1 test 1 2 1 1 test 1 1 2 1 test 5 1 1 1 test 8 1 1 1 test 1 4 1 1 test 1 4 2 1 test 1 1 1
這是我的期望的結果。
高興你到底琢磨出來。你的原始問題絕不暗示這是你以後的結果。 – Treffynnon 2011-03-01 17:48:16
我的道歉原來是Treffynnon不夠清楚,謝謝你在回覆中所做的努力。我仍然在學習,但將來會更加精確地處理我的問題。 :-) – Chris 2011-03-02 15:42:50