这是场景:我有表:
文章(articleId、userId、title、datePosted)
ArticleTranslations (languageId, articleId, title)
ArticleSections (articleSectionId, articleId, sectionId)
部分(sectionId,内容,...)
sectionAdditionalInfo (sectionId, isApproved)
我正在做的是以某种方式从 userId 的文章中选择一些文章:
SELECT article.articleId, article.userId, ArticleTranslations.title, article.datePosted
FROM Articles
LEFT OUTER JOIN ArticleTranslations ON Article.articleId= ArticlesTranslations.articleId AND ArticlesTranslations.languageId=@languageId
WHERE Articloes.userId=@userId
ORDER BY
CASE WHEN @sortDirection = 'DD' THEN datePosted END DESC, -- by date posted
CASE WHEN @sortDirection = 'DA' THEN datePosted END,
CASE WHEN @sortDirection = 'ND' THEN title END DESC, -- sort by name
CASE WHEN @sortDirection = 'NA' THEN title END,
CASE WHEN @sortDirection = 'SD' THEN ArticleTranslations.isApproved END DESC, -- is article aproved?
CASE WHEN @sortDirection = 'SA' THEN ArticleTranslations.isApproved END,
CASE WHEN @sortDirection = 'ID' THEN areAllSectionsApproved END DESC, -- sort by information if sections are all approved - within the article?
CASE WHEN @sortDirection = 'IA' THEN areAllSectionsApproved END
请记住,为了让我的问题更容易理解,我遗漏了一些信息。
现在,我想做的是选择另一个属性(对于上面 SQL 中返回的每篇文章):areAllArticleSectionsApproved
我已经单独组装了 SQL,但我希望为每一行返回这个:
SELECT CASE WHEN COUNT(sectionsAdditionalInfo.sectionId) > 0 THEN 0 ELSE 1 END AS areAllSectionsApproved
FROM ArticleSections
LEFT OUTER JOIN sectionsAdditionalInfo ON ArticleSections.sectionId = sectionsAdditionalInfo.sectionId
WHERE articleId=@articleId AND sectionsAdditionalInfo.isApproved=0
我曾尝试以某种方式嵌套此 SQL:
SELECT (outer SQL) .....
a.*(
nested SQL - second one I posted here
) as a
但它根本没有用。
我正在使用 SQL Server 2008。
任何有关如何解决此问题的提示将不胜感激;)