Monday, 15 April 2013

sql - MySQL : How to join another table with the same table but different condition -



sql - MySQL : How to join another table with the same table but different condition -

i want display school pupil graduated from. have table of school name , table of pupil profile. here's code:

school_db

shc_id shc_title 1 school 2 school b 3 school c 4 school d 5 school e

student_db

stu_id stu_school1 stu_school2 stu_school3 1 1 2 2 2 1 2 4 3 2 2 4

so write:

select school_db.sch_title school school_db inner bring together student_db on student_db.stu_school1=school_db.shc_id inner bring together student_db on student_db.stu_school2=school_db.shc_id inner bring together student_db on student_db.stu_school3=school_db.shc_id student_db.stu_id='1'

but failed right result. guys please suggest how utilize proper bring together in case.

i expect result like:

stu_id stu_school1 stu_school2 stu_school3 1 school school b school b 2 school school b school d 3 school b school b school d

regards,

you should joining table school_db thrice on table student_db can values each column on table student_db.

one more thing, should unique define alias on table school_db server can identify tables , columns has been joined.

select a.stu_id, b.shc_title sc1, c.shc_title sc2, d.shc_title sc3 student_db inner bring together school_db b on a.stu_school1 = b.shc_id inner bring together school_db c on a.stu_school2 = c.shc_id inner bring together school_db d on a.stu_school3 = d.shc_id a.stu_id = '1' sqlfiddle demo

to farther gain more knowledge joins, kindly visit link below:

visual representation of sql joins

mysql sql table inner-join

No comments:

Post a Comment