帮助限制连接的mysql数据库查询

时间:2011-03-01 14:23:50

标签: php mysql database codeigniter

我编写了一个查询,它使用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

BUT!

我试图弄清楚如何将文章限制为特定数量的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

这是我想要的结果。

4 个答案:

答案 0 :(得分:1)

好的,所以最后我弄清楚它是如何可能的。

以为我会把它包含在这里以防其他人遇到同样的问题。

更改此行


FROM articles AS a

到这个


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

做我想要的。

答案 1 :(得分:0)

那么,为什么不在SQL查询中使用 OFFSET 0,10 LIMIT * number_of_results *? (如果我理解了这个问题)

答案 2 :(得分: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>将是您希望每页显示的记录数。

答案 3 :(得分:0)

ID的具体数量...... WHERE ID IN(2,4,6,8)......?

您使用的是codeigniter的分页吗? http://codeigniter.com/user_guide/libraries/pagination.html