如何按子表筛选MS SQL查询

时间:2017-08-28 06:07:08

标签: sql sql-server

我对SQL不是很熟悉。我有一个以下数据库结构:Application,其N计为ApplicationSteps,但每个ApplicationStep有一个CaseStep,每个CaseStep都有{{1}例如,使用键1和2进行枚举。

请任何人都可以帮我完成所有StepType,其中Applications没有ApplicationApplicationStep

我的数据库结构如下:

CaseStep.Id = 5 AND CaseStep.StepType = 2

我当前的SQL查询:

1. Application
   - Id
   - Many(ApplicationStep)
2. ApplicationStep
   - Id
   - ApplicationId
   - CaseStepId
3. CaseStep
   - Id
   - StepType
   - Many(ApplicationStep)

1 个答案:

答案 0 :(得分:3)

如果我理解正确,您可以使用NOT EXISTS

SELECT a.*
FROM Application a
WHERE NOT EXISTS (SELECT 1
                  FROM ApplicationSteps aps JOIN
                       CaseStep cs
                       ON aps.CaseStepId = c.CaseStepId
                  WHERE aps.ApplicationId = a.ApplicationId AND
                        cs.CaseStepId = 5 AND cs.StepType = 2
                 )
ORDER BY a.CreationDateTime DESC
FETCH FIRST 10 ROWS ONLY ;