[英]Help with limiting a joined mysql database query
我編寫了一個查詢,該查詢使用WHERE a.id =?返回整個記錄或單個文章的正確的多對多連接的所有記錄。
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
這是我想要的結果。
好的,所以最后我弄清楚了這是怎么可能的。
以為如果有人遇到同樣的問題,我會在這里包括在內。
改變這條線
FROM articles AS a
對此
FROM (SELECT * FROM articles LIMIT 5,3) AS a
做了我想要的。
那么,為什么不在SQL查詢中使用OFFSET 0,10和LIMIT * number_of_results *呢? (如果我理解這個問題)
您可以使用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>
是您希望每頁顯示的記錄數。
ID的特定數量... ID IN(2,4,6,8)...在哪里?
您是否正在使用codeigniter的分頁? http://codeigniter.com/user_guide/libraries/pagination.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.