Search code examples
mysqloutsystems

Issue understanding the Inner Join Or Left join


I am having an issue understanding the inner and left join

I am having the below query in the outsystems

 SELECT {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments]
      , {CLD}.[LastUpdateOn],min({Project}.[Number])
      ,count({Project}.[Number])
 FROM {CLD}
     INNER JOIN {Project} ON  {Project}.[Id] = {CLDProjects}.[ProjectId] 
     INNER JOIN {CLDProjects} ON {CLD}.[Id] = {CLDProjects}.[CLDId]
 WHERE 
   (
     @IsJAXPM =1 
     or EXISTS (SELECT 1 
             FROM {CLDParticipant} 
             WHERE {CLDParticipant}.[CLDId] = {CLD}.[Id] 
               AND {CLDParticipant}.[UserId] = @UserId) 
     or EXISTS (SELECT 1 
                FROM {ProjectParticipantWidget} 
                    INNER JOIN {ProjectParticipant} ON {ProjectParticipantWidget}.[ProjectParticipantId] =  {ProjectParticipant}.[Id]
                WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id] 
                  AND {ProjectParticipant}.[UserId] = @UserId)
   )
 GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]

The issue is the Select is pulling all the CLD elements without respect to the Project, I am trying to select CLD's whose Project id = Project.Id. I tried both the joins but it keep pulling all the values Below how the structure looks like enter image description here


Solution

  • Please try the following: First get the CLDProjects matching with Project then get the CLD from matched records.

    FROM {CLD} INNER JOIN (
        {CLDProjects} INNER JOIN {Project} ON  {Project}.[Id] = {CLDProjects}.[ProjectId]
      ) ON {CLD}.[Id] = {CLDProjects}.[CLDId]