views:

202

answers:

2

I have two tables tbl_studentapplicationdetails and tbl_studentqualification... My second table contains studentId and qualificationId... Now i want to join two tables to get list of students with their qualification...

NOTE: Some students may have two or three qualification.... And my query is

select distinct t1.dStudent_id,t1.dStuFirstname,t1.dStuLastName,t1.dGender,
t1.dFatherName,t1.dOcupation,
t1.dAddress,t1.dContactNumber,t2.dMarks as sslc,t3.dMarks as hsc,
t4.dMarks as diplomo from tbl_studentapplicationdetails as t1
inner join tbl_studentqualification as t2 on t2.dQualification = '1'
inner join tbl_studentqualification as t3 on t3.dQualification = '2'
left  join tbl_studentqualification as t4     on t4.dQualification = '7' 

And my tbl_studentqualification is

alt text

EDIT: I want to select all the records for students having two qualifications and students having three qualification....

A: 

GROUP_CONCAT should solve your problem. See http://dev.mysql.com/doc/refman/5.5/en/group-by-functions.html#function_group-concat

Klette
+1  A: 

As of now, your joins make less sense because you are not joining the qualification table with the application details at all. Given that you want the student id to be distinct, you will only get one line per student.

If I understood what you want correctly, you want to get information for each type of qualification (1,2,7) in one student row. If that is the case, you should rather try something like this (I hope that is correct):

SELECT dStudent_id, dStuFirstname, dStuLastName, dGender,
       dFatherName, dOcupation, dAddress, dContactNumber,
       q1.dMarks as sslc, q2.dMarks as hsc, q7.dMarks as diplomo
FROM tbl_studentapplicationdetails
LEFT JOIN tbl_studentqualification as q1
    ON ( q1.dStudent_id = dStudent_id AND q1.dQualification = '1' )
LEFT JOIN tbl_studentqualification as q2
    ON ( q2.dStudent_id = dStudent_id AND q2.dQualification = '2' )
LEFT JOIN tbl_studentqualification as q7
    ON ( q7.dStudent_id = dStudent_id AND q7.dQualification = '7' )
poke
@poke that worked...
chandru_cp