Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4030784
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:30:56+00:00 2026-05-20T11:30:56+00:00

I have written a query which returns all records with some many-to-many joins correctly

  • 0

I have written a query which returns all records with some many-to-many joins correctly for the entire set or an individual article using 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!

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

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'];
    }       

}

But if I just use LIMIT (with offset etc) 1

the results I get are

---------------------------------------------------------------------
id     title        category_id       person_id       organization_id
---------------------------------------------------------------------
1      test              1                1                  1

instead of

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

which is my desired result.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:30:56+00:00Added an answer on May 20, 2026 at 11:30 am

    OK, so finally I worked out how it is possible.

    Thought i’d include it here in case anyone else has the same problem.

    Changing this line

    
    FROM articles AS a
    

    to this

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

    does what I wanted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a query which involves joins and finally returns the below result,
I have written some code which sends queries to google and returns the query
I have Written a query which returns multiple column.Out of which One Column contains
I have written this query which returns a users friends and the friends on
I am working on SQLite and I have written a query which returns me
I have a query and a loop written that lists all the rows from
I have written method which should return string with data from select query, but
I have a query which returns to me results as follows: Race | Candidate
Afternoon, I have a LINQ query, which counts all products with a certain criteria.
i have written insert query for my application to create new user with password,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.