?
Solved

PHP Join and filter from multiple tables

Posted on 2009-05-06
4
Medium Priority
?
704 Views
Last Modified: 2012-05-06
Hi Experts

Would appreciate some wise words, can't get my head around this one.

I have 3 tables:
students
groups
group_assignments

Basically the student table contains all the info, the groups table lists a simple list of available groups and the group_assignments table lists details of which student belongs to which group.

I am currently returning a complete list of students with no problem but I would like to be able to include the student group details in the recordset and filter on it.

Simple enough if each student only belongs to one group but most of them belong to many. What would be the best approach?
0
Comment
Question by:sirnutty1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
aconrad earned 2000 total points
ID: 24319520
below query will return a result set with 3 columns :
name,group_ids,group_name

And the data
A, "33,11" , "g3,g1"
B, "33,22,11" , "g3,g2,g1"
C, "33,22" , "g3,g2"

Then you can then use php's explode to get each student's group
I attached a mysql dump of the tables i used.

http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_group-concat
You need mysql5 to use group concat.

The mysql4 query would be without group_by and group_concat

SELECT
 s.name,
 group_ids,
 group_name
 
FROM students AS s
INNER JOIN group_assignments AS ga ON (s.student_id=ga.student_id)
INNER JOIN groups AS g ON (ga.group_id=g.group_id)

Then in the php code use array keys to add each group where it belongs
Something like this:


$out = array();
while ($row = mysql_fetch_assoc($result)) {
  $out[$row['name']][] = $row['group_name']
}
print_r($out);

best approach in my opinion is mysql5




SELECT 
 s.name,
 GROUP_CONCAT(ga.group_id) AS group_ids,
 GROUP_CONCAT(g.group_name) AS group_name
 
FROM students AS s 
INNER JOIN group_assignments AS ga ON (s.student_id=ga.student_id)
INNER JOIN groups AS g ON (ga.group_id=g.group_id)
 
GROUP BY ga.student_id

Open in new window

0
 
LVL 4

Expert Comment

by:aconrad
ID: 24319552
I cant upload .sql  files, you'll need to rename exp-export.txt to exp-export.sql ....
exp-export.txt
0
 

Author Comment

by:sirnutty1
ID: 24326378
Many thanks for your answer aconrad, works perfectly!!

Can you just clarify how I would then perform a filter on the group_concat columns?

Would I use a WHERE clause in the mySQL query or something else?

0
 

Author Comment

by:sirnutty1
ID: 24326817
Just figured out a simple addition to the where clause will do it!!

Many thanks again
0

Featured Post

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to implement server side field validation and display customized error messages to the client.
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question