简体   繁体   English

SQL Select忽略子查询的结果

[英]SQL Select omitting results from a sub-query

I have the following code which, when run separately, produces the results I expect but when run collectively returns nothing: 我有以下代码,当单独运行时,它们会产生预期的结果,但是当共同运行时,则不返回任何结果:

SELECT SurveyUID FROM   tbSurvey AS tbS
WHERE  NOT EXISTS   
(SELECT tbS.SurveyUID
FROM         tbSurvey AS tbS INNER JOIN
                  tbSurveyElementCategory AS tbSEC ON tbS.SurveyUID = tbSEC.SurveyUID INNER JOIN
                  tbElementCategory AS tbEC ON tbSEC.ElementCategoryID = tbEC.ElementCategoryID
WHERE     (tbEC.ElementCategoryID = 75))

The first line returns 185 records and the sub-query returns 20 records as a sub-set of the first query. 第一行返回185条记录,子查询返回20条记录作为第一条查询的子集。 I am trying to return the 165 records that don't appear in the sub-query. 我试图返回未出现在子查询中的165条记录。 Thanks 谢谢

You forgot put tbS.SurveyUID = tbS1.SurveyUID condition 您忘记输入tbS.SurveyUID = tbS1.SurveyUID条件

Try this: 尝试这个:

SELECT SurveyUID 
FROM   tbSurvey AS tbS
WHERE NOT EXISTS (SELECT tbS1.SurveyUID
                  FROM tbSurvey AS tbS1 
                  INNER JOIN tbSurveyElementCategory AS tbSEC ON tbS1.SurveyUID = tbSEC.SurveyUID 
                  INNER JOIN tbElementCategory AS tbEC ON tbSEC.ElementCategoryID = tbEC.ElementCategoryID
                  WHERE (tbEC.ElementCategoryID = 75) AND tbS.SurveyUID = tbS1.SurveyUID
                 )

Your query is wrong. 您的查询是错误的。 You have to write: ...WHERE SurveyUID NOT IN (...subquery...) . 您必须编写: ...WHERE SurveyUID NOT IN (...subquery...) In your query you say "give me all ID-s if the subquery has no results" but it has results. 在查询中,您说“如果子查询没有结果,请给我所有ID”,但有结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM