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.
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:
This will give you the last 30 days.
Alternatively, you can display only a certain number of records at a time using LIMIT:
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:
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?