mysql - Trouble querying results from three tables -
i'm trying query data practice , these 3 tables:
course assignment assignment_submissions id id id course assignment
as can see relationships pretty basic, i'm rusty plain sql, that's why i'm practicing.
given course 'id'
, how can find assignment_submission
rows belong it?
i need assignments, each assignment each submissions. here's attempt, fails , returns larger dataset expected.
select sub.id, sub.grade, uv_assignment_submissions sub, uv_assignment ass ass.course = 1245
you need join
3 tables, course
, assignment
, assignment_submissions
. have used join
construct assuming need display course details when records exists assignments
, assignment_submissions
.
the reason why query fails because, getting cartesian
product of records 2 tables
, need condition specify rows valid current assignment
specifying join
condition
something should work
select c.id, asub.id course c join assignment on ( c.id = a.course ) join assignment_submissions asub on ( asub.id = a.assignment ) c.id = 10 --your course id goes here or c.id in (10, 20, 30) -- multiple ids
more on sql joins: http://dev.mysql.com/doc/refman/5.0/en/join.html
Comments
Post a Comment