Munchmallow Munchmallow - 28 days ago 7
SQL Question

Query code shows nothing after adding a new relationship

I have relationship between

Student.Department
and
Departments.Id
,
Student.Faculty
and
Faculties.Id
. Below code was working before I created a new relationship between
Departments.facultyId
and
Faculties.Id


SELECT
*
FROM
Students sd
INNER JOIN Departments dp
ON sd.Department = dp.Id
INNER JOIN Faculties fd
ON sd.Faculty = fd.Id


Before adding
facultyId
column in
Departments
table, I could run the query, but now the query shows nothing.

Should I also write something for relationship between
fd.Id
and
dp.facultyId
?

Designer

Answer

First, verify this join:

SELECT 
    * 
FROM 
    Students sd 
    LEFT JOIN Departments dp 
        ON  sd.Department = dp.Id

If cannot see Departments information, verify that sd.Department has the correct values.

Do the same with Faculties

SELECT 
    * 
FROM 
    Students sd 
    LEFT JOIN Faculties fd 
        ON sd.facultyId = fd.Id

Using LEFT JOIN you'll see all records of the first table, and only the records of second table that match the ON clause.

And finally check the new relation:

SELECT 
    * 
FROM 
    Dapartments dp 
    LEFT JOIN Faculties fd 
        ON dp.facultyId = fd.Id
Comments