R.JN R.JN - 5 months ago 9
SQL Question

how can i use a join for two columns corresponding to the same primary key

i have a table have five columns:

streetId,streetName, areaId, ISfSectionId1, ISFsectionId2


where
areaId
is a foreign key for table
area
and
isfsectionid1
and
isfsectionid2
are foreign keys for the same primary key
isfsectionId
(isfsectionId2 have null value)
am using this query to join them

SELECT s.streetId,s.streetName,a.areaName,i.isfsectionName,d.ISFsectionName
from area a inner join
street s on s.areaId = a.areaId inner join
ISFsections i on s.fasileone = i.ISFsectionId join
ISFsections d on s.fasiletwo = d.ISFsectionId


without last join is working good but when adding the last join its giving me records for the ISFsectionId2 only
what is the problem

Answer
SELECT s.streetId
      ,s.streetName
      ,a.areaName
      ,i.isfsectionName
      ,d.ISFsectionName
from       area        a 
INNER join street      s on s.areaId = a.areaId 
LEFT join  ISFsections i on s.fasileone = i.ISFsectionId 
LEFT join  ISFsections d on s.fasiletwo = d.ISFsectionId
Comments