Jump to content


Photo

Query from multiple tables probably simple


  • Please log in to reply
4 replies to this topic

#1 tgelston

tgelston
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 March 2006 - 03:04 PM

I am stumped - I started an ambitious project of creating my own online gradebook and I love the challenge of learning as I go. . but now I am stuck! Here is what I want to do

I need to click on a project title and be presented with a list of all my students and the grades they received for that project. I have two tables, studentGrade that holds project id, student id and grade AND userInfo that holds the student id, names and other info of all my students. This is a join. . . I have done other, simple joins before. . I just can't seem to wrap my brain around what I need to do here. I want all students listed - even if they do not have a grade.

example output would look something like

john smith 89

mary jane

Tom Kat 54

I have this

$query='SELECT * FROM userInfo, studentGrade, assignmentInfo WHERE studentGrade.project_id = 45 AND assignmentInfo.project_id = 45';

But it outputs all students 3x with the grade filled in for every student

Hints, suggestion and tips would all be very much appreciated.





Thank You,
Tobias

#2 jvrothjr

jvrothjr
  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts
  • LocationOhio

Posted 31 March 2006 - 04:34 PM

$query = ("select * from userInfo
    left join studentGrade on userInfo.project_id=studentGrade.project_id
    where studentGrade.project_id = 45");

If you understand everything you know nothing!

http://rcchjr.awardspace.com/

#3 tgelston

tgelston
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 March 2006 - 05:18 PM

Thank you for your reply - Looking at your code I see userInfo.project_id - I don't have a project_id field in my userInfo tbl? IS this a typo or am I not understanding your join syntax. I tried your code both as you have written it and as my example below


[!--quoteo(post=360404:date=Mar 31 2006, 11:34 AM:name=jvrothjr)--][div class=\'quotetop\']QUOTE(jvrothjr @ Mar 31 2006, 11:34 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$query = ("select * from userInfo
    left join studentGrade on userInfo.project_id=studentGrade.project_id
    where studentGrade.project_id = 45");
[/quote]


$query = 'SELECT * from assignmentInfo, userInfo LEFT JOIN studentGrade on assignmentInfo.project_id=studentGrade.project_id WHERE studentGrade.project_id = 45';

But I still get the output three times. . all students are listed.. . I need to go read up on joins?

I appreciate the help!



#4 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 31 March 2006 - 06:44 PM

When you do a join, each joined table needs what we call a "join condition". Using the JOIN ... ON ... syntax will make missing join conditions stand out like a sore thumb:

SELECT * 
FROM userInfo u 
LEFT JOIN studentGrade g ON g.student_id=u.student_id AND g.project_id = 45
LEFT JOIN assignmentInfo a ON a.project_id=g.project_id
First, I gave each table an alias (`a`, `g`, and `u`) to make it easier to write the query.

The condition after "ON" is the join condition, it links the table you are joining into the ones that have come before.

Now, because you want every student to be listed, you should put userInfo first and put LEFT JOINs after it. And since studentGrade is the linking table between students and assignments, it should go second.

Finally, since there is no link for students who haven't been graded, using that "WHERE g.project_id=45" will make ungraded students disappear. So I moved it out of the WHERE and into the LEFT JOIN condition.

Note that the assignment info will be missing on students that aren't graded. It might be best to get the assignment info in a separate query, or you could do it this way:

SELECT * 
FROM userInfo u 
LEFT JOIN studentGrade g ON g.student_id=u.student_id AND g.project_id = 45
LEFT JOIN assignmentInfo a ON a.project_id=45


#5 tgelston

tgelston
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 31 March 2006 - 10:13 PM

Perfect - Thank you very much! I appreciate you taking the time to explain your answer.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users