gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:22:47 25 4
gpt4 key购买 nike

我编写了一个查询,它使用 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_nameFROM articles AS aLEFT OUTER JOIN articles_categories AS ac ON a.id=ac.article_idLEFT OUTER JOIN categories AS c ON c.id=ac.category_idLEFT OUTER JOIN articles_people AS ap ON a.id=ap.article_idLEFT OUTER JOIN people AS p ON p.id=ap.person_idLEFT OUTER JOIN articles_organizations AS ao ON a.id=ao.article_idLEFT OUTER JOIN organizations AS o ON o.id=ao.organization_idORDER BY date_added

BUT!

I've hit a brick wall trying to work out how to limit the articles to a specific number of IDs, for working with pagination.

I'm ideally trying to use as simple and clear SQL statements as possible because I'm using the codeigniter framework with their active record class.http://codeigniter.com/user_guide/database/active_record.html

Would really appreciate some help as I don't want to revert to using multiple queries for this as I've tried to reduce it down to a single query for database efficiency.

Have search around and tried some alternatives but nothing seems to work. Many thanks!

For example the results I return are like this

---------------------------------------------------------------------id     title        category_id       person_id       organization_id---------------------------------------------------------------------1      test              1                1                  11      test              2                1                  11      test              1                2                  11      test              1                1                  21      test              5                1                  11      test              8                1                  11      test              1                4                  11      test              1                4                  21      test              1                1                  12      test 2            2                1                  12      test 2            1                2                  12      test 2            1                1                  22      test 2            5                1                  12      test 2            8                1                  12      test 2            1                4                  12      test 2            1                4                  2

I need the results like this so that I can create sub-arrays in the php like this:


$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                  11      test              2                1                  11      test              1                2                  11      test              1                1                  21      test              5                1                  11      test              8                1                  11      test              1                4                  11      test              1                4                  21      test              1                1                  1

这是我想要的结果。

最佳答案

好的,所以我终于弄明白了这是怎么可能的。

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

改变这一行


FROM articles AS a

对此


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

做我想做的。

关于php - 帮助限制连接的 mysql 数据库查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5155777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com