2011-03-01 62 views
0

我已經寫了一個查詢,它返回的所有記錄都使用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 

這是我的期望的結果。

回答

1

好的,所以最後我想出了它是如何實現的。

以爲我會在這裏包括它,以防其他人有同樣的問題。

更改此行


FROM articles AS a 

這個


FROM (SELECT * FROM articles LIMIT 5,3) AS a 

做什麼,我想要的。

+0

高興你到底琢磨出來。你的原始問題絕不暗示這是你以後的結果。 – Treffynnon 2011-03-01 17:48:16

+0

我的道歉原來是Treffynnon不夠清楚,謝謝你在回覆中所做的努力。我仍然在學習,但將來會更加精確地處理我的問題。 :-) – Chris 2011-03-02 15:42:50

0

那麼,你爲什麼不使用OFFSET 0,10LIMIT * NUMBER_OF_RESULTS *在SQL查詢? (如果我理解了這個問題)

+0

謝謝Runeroniek,我剛剛更新了我的問題,使其更加清晰。希望這解釋了爲什麼單獨使用LIMIT不會返回我想要的結果? – Chris 2011-03-01 15:40:26

0

您可以輕鬆地限制使用MySQL LIMIT子句返回的記錄數。這可以通過您的示例查詢來實現。

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 
LIMIT 10 

其中10是您希望顯示的記錄數。 MySQL LIMIT子句允許您指定記錄數量和初始偏移量的限制。像這樣:

LIMIT <offset>,<limit> 

在你的情況<offset>將當前頁面*的頁面上的記錄數。 <limit>將是您希望每頁顯示的記錄數。

+0

嗨,我已經更新了我的問題,使它更清晰一點。我試圖限制文章不僅僅是記錄的數量,而是將id的數量乘以每個id的結果數量。希望這更有意義? – Chris 2011-03-01 15:39:31

0

ID的具體數量...哪裏ID IN(2,4,6,8)...?

你使用codeigniter的分頁? http://codeigniter.com/user_guide/libraries/pagination.html

+0

嗨,我已經更新了我的問題,以更好地說明這一點。 我可以使用WHERE查找單個文章的詳細信息,但我希望能夠使用鏈接的codeigniter分頁進行分頁,我認爲這需要限制和偏移量,或者等效於函數。 感謝您的幫助! – Chris 2011-03-01 15:34:47

+0

你好,你可以使用一些選擇,而不是很多連接,或者,如果你想用你的查詢嘗試子查詢: WHERE a.id IN(SELECT ID從文章LIMIT 5) – Marsio 2011-03-09 15:39:44