0

I have multiple tables (tbldept, tblcourse, tblstud, tblviolation) and I want to extract specific values. The below tables is like the same on my tables

tbldept
id | dept
1  | deptA
2  | deptB

tbldept has foreign key on tblcourse

tblcourse
id | deptId | course
1  |    2     | courseA
2  |    1     | courseB
3  |    1     | courseC

tblcourse has foreign key on tblstud

tblstud
id | courseId | name 
1  |   1      | studA
2  |   2      | studB
3  |   1      | studC

tblstud has foreign key on tblviolation

tblviolation
id | studId | violationName
1  |   3    |  violationA
2  |   2    |  violationB
3  |   1    |  violationC
4  |   3    |  violationC

*What I want to get is look like this: *

dept | studId | name   | violationName
  2  |    1   | studA  | violationC
  2  |    2   | studB  | violationB
  1  |    3   | studC  | violationA
  1  |    3   | studC  | violationC

I want to get all the rows of tblviolation for each studId. I hope you guys understand what I am trying to explain. =) Thank you.

1
  • FWIW, I find the convention of sticking 'tbl' in front of everything absolutely infuriating. What's wrong with 'departments', 'students', and 'courses'? Commented Sep 20, 2017 at 5:27

1 Answer 1

1

You just need inner join. Try this.

select d.dept,s.studid,s.name,v.violationname
from tbldept d 
inner join tblcourse c
on d.id=c.deptid
inner join tblstud s
on c.id=s.courseid
inner join tblviolation v
on s.id=v.studid
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.