2011-06-15 36 views
1
Select FileUpload.FileName AS FINAME, FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy from FileUpload 
    INNER JOIN 
    ContentManagement ON ContentManagement.FileId=FileUpload.FileId 
    INNER JOIN 
    MemberPersonalInformation ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
    INNER JOIN 
    SubjectMaster ON ContentToIndividual.SubjectId=SubjectMaster.SubjectId 
    where 
    FileUpload.FileId in 
    (Select FileId from ContentManagement where ContentId in 
    (Select ContentId from ContentToIndividual where ShowToMemberId=12) 
    AND ContentManagement.ContentTypeId=1 and ContentManagement.SessionId=4) 

加入雖然我執行這個查詢在最後得到一個錯誤的JOIN說The multi-part identifier "ContentToIndividual.SubjectId" could not be bound.但我確實有兩個tables.I SubjectId無法理解什麼問題。請幫助我。問題與內蒙古在SQL服務器2005

+1

如果您使用的是SQL管理工作室,則始終可以使用查詢設計器來解決此類問題。它幫助我分配時間 – Ivo 2011-06-15 11:05:24

+0

消除子查詢是否加快了速度? – 2011-06-15 11:37:23

回答

4

要加入SubjectMaster表到ContentToIndividual表,您以前沒有引用。

您需要加入到contentToIndvidual才能參考SubjectMaster加入。

例如

Select FileUpload.FileName AS FINAME, 
     FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy 
from FileUpload 
    INNER JOIN 
    ContentManagement ON ContentManagement.FileId=FileUpload.FileId 
    INNER JOIN 
    MemberPersonalInformation ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
    -- You need to add it in here 
    Inner Join ContentToIndividual on SomeColumn = AnotherColumn 
    INNER JOIN 
    SubjectMaster ON ContentToIndividual.SubjectId=SubjectMaster.SubjectId 
    where 
    FileUpload.FileId in 
    (Select FileId from ContentManagement where ContentId in 
    (Select ContentId from ContentToIndividual where ShowToMemberId=12) 
    AND ContentManagement.ContentTypeId=1 and ContentManagement.SessionId=4) 

注意:即使你是在一個子查詢中查詢ContentToIndividual你不能引用列,如果它不是主要的選擇查詢的一部分。

+0

謝謝先生!!!!!!!!!!!!! – Naresh 2011-06-15 11:21:44

3

您還沒有加入您的主要select語句中的ContentToIndividual。您需要添加或不參考它。

編輯:只需添加,你實際上不需要在主選擇中添加SubjectMasterContentToIndividual連接,因爲你沒有從任一表中選擇任何列 - 請記住,子查詢與主查詢;你只用它來獲取FileIds列表。也可以優化聲明的其餘部分。

Select FileUpload.FileName AS FINAME, FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy from FileUpload 
    INNER JOIN 
    ContentManagement ON ContentManagement.FileId=FileUpload.FileId 
    INNER JOIN 
    MemberPersonalInformation ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy 
    where 
    FileUpload.FileId in 
    (Select FileId from ContentManagement where ContentId in 
    (Select ContentId from ContentToIndividual where ShowToMemberId=12) 
    AND ContentManagement.ContentTypeId=1 and ContentManagement.SessionId=4) 

編輯2:只是爲了好玩,我覺得這可能會簡化事情有點,因爲它擺脫了子查詢的,所以它應該是更快...

SELECT  FileUpload.FileName AS FINAME, FileUpload.FilePath,MemberPersonalInformation.FirstName As SharedBy 
FROM  FileUpload 
INNER JOIN ContentManagement ON ContentManagement.FileId=FileUpload.FileId 
      AND ContentManagement.ContentTypeId=1 
      AND ContentManagement.SessionId=4 
INNER JOIN ContentToIndividual ON ContentToIndividual.ContentId = ContentManagement.ContentId -- Iguessed at this join 
      AND ContentToIndividual.ShowToMemberId=12 
INNER JOIN MemberPersonalInformation ON MemberPersonalInformation.MemberId = ContentManagement.CreatedBy 
+0

我沒有嘗試,但看起來不錯。我怎麼找到哪個查詢工作得很快? – Naresh 2011-06-15 12:39:08

+1

有幾種方法。如果您在MSMS中突出顯示這兩個查詢,並右鍵單擊並選擇「顯示估計的執行計劃」,那麼它將爲每個查詢提供一個分數作爲批次的百分比。例如,如果一個是20%,另一個是80%,那麼第一個可能快四倍。如果您想要更準確的答案,請選擇「包括實際執行計劃」,這將在運行兩個查詢(通過按F5)後在結果窗口中顯示爲額外的選項卡。 – 2011-06-15 13:00:25

+0

當然,這不一定會導致實際的速度差異。要獲得實際執行時間的更長時間的方法,請提出一個新問題,我會給你一個更好的答案。 – 2011-06-15 13:01:29