2010-10-21 63 views
1

我有一個SQL查詢,我現在無法改進。它有效,但有點難看。使用WHERE子句多次使用相同的子查詢改進查詢

我想獲取:使用這本身取從另一個SQL查詢值的WHERE子句

  • 從表ID的
  • 從表B中

的名稱。

我想換成用於WHERE子句此SQL查詢以下兩種情況下,用一個實例:

SELECT intImageGalleryID FROM tblEPiServerCommunityImageGalleryImage 
WHERE intID = 123123 

如何能不能做到?

使用SQL Server。

下面是完整的SQL查詢:

SELECT intID, 
     (SELECT strName 
     FROM tblEPiServerCommunityImageGallery 
     WHERE intID = 
      (SELECT intImageGalleryID 
      FROM tblEPiServerCommunityImageGalleryImage 
      WHERE intID = 123123) 
      ) as name 
FROM tblEPiServerCommunityClub 
    WHERE intImageGalleryID = 
     (SELECT intImageGalleryID 
     FROM tblEPiServerCommunityImageGalleryImage 
     WHERE intID = 123123) 

謝謝!

回答

2

你可以試試下面的查詢。我認爲這與您的原始解決方案相同。

SELECT scc.intID 
     , sci.strName 
FROM tblEPiServerCommunityClub scc 
     INNER JOIN tblEPiServerCommunityImageGalleryImage scig ON scig.intImageGalleryID = scc.intImageGalleryID 
     INNER JOIN tblEPiServerCommunityImageGallery sci ON sci.intID = scig.intImageGalleryID 
WHERE scig.intID = 123123   
+0

感謝列文 - 不幸的是,返回的intId是123123 - 在WHERE子句中非常相似。如何返回實際的intImageGalleryId? – 2010-10-21 06:38:29

+0

@Martin S.看來我使用了錯誤的別名。你可以重試嗎? – 2010-10-21 06:42:56

+0

它正在工作!謝謝你!現在這取決於我想明白這一點:)乾杯! – 2010-10-21 06:49:38