Rashmi
Rashmi

Reputation: 551

MySql Join Query left join

I have table name students having fields student_id, house_id etc.. and subjects, houses, students_subjects

I am using this query

SELECT students_subjects.student_id,students.house_id,students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name FROM 
students_subjects LEFT JOIN students on students_subjects.student_id=students.id
LEFT JOIN subjects on students_subjects.subject_id=subjects.id  WHERE students_subjects.class_years_section_id=1 

This is working fine for me ..

Now i want to get house name too from houses table

I tried this query

SELECT students_subjects.student_id,students.house_id,houses.house_name, students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name FROM 
students_subjects LEFT JOIN students on students_subjects.student_id=students.id
LEFT JOIN subjects on students_subjects.subject_id=subjects.id 

LEFT JOIN houses on students.house_id=houses.id WHERE students_subjects.class_years_section_id=1

AND students_subjects.school_session_id=1 AND students.is_active=1

and it gives me house_name = NULL

Can anybody tell me how to get house name too . using join query

Thanks

Upvotes: 1

Views: 1042

Answers (2)

John Woo
John Woo

Reputation: 263933

The error in your query is caused by the LEFT JOIN keyword that is after WHERE clause,

SELECT  students_subjects.student_id,
        students.house_id,
        students_subjects.subject_id,
        subjects.subject_name,
        students.rollno,
        students.first_name, 
        students.last_name 
FROM    students_subjects 
        LEFT JOIN students 
            on students_subjects.student_id=students.id
        LEFT JOIN subjects 
            on students_subjects.subject_id=subjects.id
        LEFT JOIN houses 
            on students.house_id=houses.id
WHERE   students_subjects.class_years_section_id = 1 AND 
        students_subjects.school_session_id = 1 AND 
        students.is_active = 1

Remember that JOINs are part of the FROM Clause.


UPDATE 1

SELECT  b.student_id,
        a.house_id,
        b.subject_id,
        c.subject_name,
        a.rollno,
        a.first_name, 
        a.last_name,
        d.house_name            
FROM    students a
        INNER JOIN students_subjects b
            ON b.student_id = a.id
        INNER JOIN subjects  c
            ON b.subject_id = c.id
        INNER JOIN houses d
            ON a.house_id = d.id
WHERE   b.class_years_section_id = 1 AND 
        b.school_session_id = 1 AND 
        a.is_active = 1

Upvotes: 7

Dion D.
Dion D.

Reputation: 2596

You've miss placed the WHERE clause, try this:

SELECT students_subjects.student_id,students.house_id,students_subjects.subject_id,subjects.subject_name,students.rollno,students.first_name, students.last_name 
FROM students_subjects 
     LEFT JOIN students ON students_subjects.student_id=students.id
     LEFT JOIN subjects ON students_subjects.subject_id=subjects.id  
     LEFT JOIN houses ON students.house_id=houses.id
WHERE students_subjects.class_years_section_id=1 
AND students_subjects.school_session_id=1 AND students.is_active=1

Upvotes: 0

Related Questions