1

我在理解内部和左连接时遇到问题

我在外系统中有以下查询

 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]

问题是 Select 正在提取所有 CLD 元素而不考虑项目,我正在尝试选择项目 id = Project.Id 的 CLD。我尝试了这两个连接,但它不断拉动所有值低于结构的样子 在此处输入图像描述

4

3 回答 3

2

请尝试以下操作:首先获取与 Project 匹配的 CLDProjects,然后从匹配的记录中获取 CLD。

FROM {CLD} INNER JOIN (
    {CLDProjects} INNER JOIN {Project} ON  {Project}.[Id] = {CLDProjects}.[ProjectId]
  ) ON {CLD}.[Id] = {CLDProjects}.[CLDId]
于 2016-06-08T16:22:01.903 回答
1

您可能在 ProjectParticipant 子查询中缺少对 CLDProjects 的内部连接。添加

INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] =  {CLDProjects}.[ProjectId])

在第二个 EXISTS 连接条件上,否则第二个结果将匹配用户所在的每个项目的存在,忽略 CLDProjects 上的其他条件。尝试以下操作:

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]

                    INNER JOIN {CLDProjects} ON {ProjectParticipant}.[ProjectId] =  {CLDProjects}.[ProjectId]

                WHERE {ProjectParticipant}.[ProjectId] = {Project}.[Id] 
                  AND {ProjectParticipant}.[UserId] = @UserId)
   )
 GROUP BY {CLD}.[Id], {CLD}.[Name], {CLD}.[Comments], {CLD}.[LastUpdateOn]

另外..确保您没有将@IsJAXPM 作为1 传递...否则它肯定会返回所有记录。让我们知道这是否有效。否则,请扩展图表以显示 ProjectParticipant 和 ProjectParticipantWidget 表。

于 2016-06-10T16:42:40.573 回答
0

我认为你只需要重新排序你的连接,我试图重现你的情况(连接顺序不正确),但是在测试查询时它给了我错误,在此处输入图像描述

在此处输入图像描述

但如果你重新排序连接,它会起作用。请注意,我使用的是 OutSystems 10

于 2016-12-09T01:34:53.923 回答