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 7872451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:12:12+00:00 2026-06-03T02:12:12+00:00

I’m trying to get an array that contains information from three tables. The result

  • 0

I’m trying to get an array that contains information from three tables. The result should be an array where I can loop through the first table, its related rows from the second and again the related rows from the third to the second. At the moment, I’m having three separate SQL queries, that then get reformed into one single array.

Maybe someone can help me with an idea of the best way to query those tables to get the array below. I currently got three queries – one for each table. With the mysql JOIN it’s quite an easy query, but I’m having problems with arranging it back to the array.

I’m thinking of using a mysql JOIN to get the same result with less queries, but actually I’m wondering whether that’s so good or even possible at all. I’ll add numbers to the approximate sizes of the tables.

Let me give you some details:

THE TABLES

courses(4000 rows)
course_id----course_date

groups(50 000 rows)
group_id----group_size----group_course_id

users(200 000 rows)
user_id----user_name----user_group_id

An example of the desired array:

[0] => Array('courseinfo' => 
              Array('course_id' => 1234,
                    'course_date' => '2012-08-12')
             'groups' =>
              Array( [0] => Array('group_id' => 345,
                                  'group_size' => 3,
                                  'group_course_id' => 1234,
                                  'users' => Array([0] => Array('user_id' => 456,'user_name' => Pete Cabrinha, 'user_group_id = '345'), 
                                                   [1] => Array('user_id' => 336,'user_name' => John Smith, 'user_group_id = '345'))
                     [1] => Array('group_id' => 345,
                                  'group_size' => 3,
                                  'group_course_id' => 1234,
                                   'users' => Array([0] => Array('user_id' => 456,'user_name' => Pete Cabrinha, 'user_group_id = '1234'), 
                                                    [1] => Array('user_id' => 336,'user_name' => John Smith, 'user_group_id = '1234'))))

Hope it’s not too complicated, just note the relation between the course_id AND group_course_id as well as the relation between group_id and user_group_id.

Heres a little snippet of the way I query at the moment:

    $data = $courses = $read->read_courses();
    $i = 0;
    foreach($courses as $course)
    {
        if($data[$i]['groups'] = $read->read_groups($course['course_id']))
        {
            $j = 0;
            foreach($data[$i]['groups'] as $group)
            {
                $data[$i]['groups'][$j]['users'] = $read->read_users($group['group_id']);
                $j++;
            }
        }
        else {$data[$i]['groups'][0] = array(); $data[$i]['groups'][0]['users'] = array();}
        $i++;
    }

And finally the JOIN I thought of using…

        SELECT 
            course.course_id,
            course.course_date,

            groups.group_id,
            groups.group_course_id,
            groups.group_size,

            user.user_name


        FROM course
        LEFT JOIN groups 
        ON course.course_id = groups.group_course_id

        LEFT JOIN user
        ON groups.group_id = user.user_group 

Hope someone can help me here, or put me in the right direction – or even come up with a way better idea.

  • 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-06-03T02:12:13+00:00Added an answer on June 3, 2026 at 2:12 am

    There are several ways to do that query as you can see in the answer, but you should never load the whole database in a PHP array. According to your comment, you need one month at a time, so add a WHERE clause to limit the results to the month you want.

    For instance:

    SELECT ...
    FROM ...
    WHERE DATE_SUB(CURDATE(),INTERVAL 30 DAY) <= date_col;
    

    This will give you the last 30 days.

    Alternatively, you can display only a certain number of records at a time using LIMIT:

    LIMIT 0, 100
    

    will give you 100 records starting at offset 0 (the first 100 records). It makes it easier to paginate.

    If you want to get the newest courses first, you use ORDER BY:

    ORDER BY course.course_date DESC
    LIMIT 0, 100
    

    Then you only need to change the limit on each query to get the next batch of records to display and of course, your queries will be very fast and use little memory because you never download more than 100 records.


    Sorry, but it sounds like you are doing the database’s work in PHP.

    Why on Earth would you load 254k entries in a PHP array? You are NOT going to display 254k records on a single page, are you? The performance would be horrendous and just a handful of users will crash your server.

    You should really re-think what data you really need and how you will use it.

    What are you trying to do? What data do you need? How can you get it from the database server?

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.