2014-04-17 84 views
0

我寫了下面的語句來抓取行從examPeriod基於條件相匹配的內部EXAMCENTERWORKINGDAYS行是檢索數據Eep.[tpId]=ecwd.[TRAININGPERIODID]使用外連接

查詢如下:

SELECT ep.[id] 
FROM [LMS17_ARU_backup_201402080000].[dbo].[examperiod]ep 
RIGHT JOIN 
    (SELECT ecwd.[trainingperiodid] 
    FROM [LMS17_ARU_backup_201402080000].[dbo].[examcenterworkingdays]ecwd 
    WHERE ecwd.[value] <> 0 
    AND ecwd.[trainingperiodid]IS NOT NULL)ecwd ON ep.[tpid] = ecwd.[trainingperiodid] 

麻煩的是, right join之後括號內的select語句只檢索95行,當我執行查詢時,它返回324行。我希望所有的查詢返回95行,也就是我想從[examPeriod]表中獲取ep.[id]從95行從嵌套查詢時返回ep.[tpId]=ecwd.[TRAININGPERIODID]

很抱歉,如果這是愚蠢的問題

+0

你有多行,因爲有每個訓練時間ID – Hituptony

+0

多個正品的我想你'RIGHT'加入是複用你的查詢結果。對於ep.tdid的一個值,您有TRAININGPERIODID的多個值。嘗試使用實際值進行驗證... – SoulTrain

回答

-1

爲什麼你需要一分-QUERY?單個查詢不會實現您想要的嗎?試試這個:

SELECT ep.[id] 
FROM [LMS17_ARU_backup_201402080000].[dbo].[EXAMCENTERWORKINGDAYS] ecwd 
LEFT JOIN [LMS17_ARU_backup_201402080000].[dbo].[examPeriod] ep 
     ON ep.[tpId] = ecwd.[TRAININGPERIODID] 
WHERE ecwd.[VALUE] <> 0 AND ecwd.[TRAININGPERIODID] IS NOT NULL 

或者這樣:

SELECT ep.[id] 
FROM [LMS17_ARU_backup_201402080000].[dbo].[EXAMCENTERWORKINGDAYS] ecwd 
LEFT JOIN [LMS17_ARU_backup_201402080000].[dbo].[examPeriod] ep 
     ON ep.[tpId] = ecwd.[TRAININGPERIODID] 
      AND ecwd.[VALUE] <> 0 
      AND ecwd.[TRAININGPERIODID] IS NOT NULL