2011-07-11 73 views
1
DECLARE @TotalQuestions int; 
DECLARE @CorrectQuestions int; 
DECLARE @IncorrectQuestions int; 

SELECT (
    SET CorrectQuestion = SELECT COUNT(WiningComment) 
    FROM Threads 
    WHERE WiningComment IN (SELECT CommentsID 
    FROM Comments 
    WHERE [email protected]) 
) as 'WinningAnswers', 
(
    SET TotalQuestions = SELECT COUNT(CommentsID) 
    FROM Comments 
    WHERE [email protected] 
) as 'TotalAnswers' 
(
    SELECT (TotalQuestions-CorrectQuestions) //I am not sure about this part!! 
) as 'IncorrectQuestions' 

我不知道最後一部分減去的結果幫幫忙,我想從另一個子查詢我需要從兩個查詢

回答

2

的結果減去一個子查詢的結果試試這個:

DECLARE @TotalQuestions int; 
DECLARE @CorrectQuestions int; 
DECLARE @IncorrectQuestions int; 

SELECT @CorrectQuestions = COUNT(WiningComment) 
    FROM Threads 
    WHERE WiningComment IN (SELECT CommentsID 
    FROM Comments 
    WHERE [email protected]) 


    SELECT @TotalQuestions = COUNT(CommentsID) 
    FROM Comments 
    WHERE [email protected] 


    SELECT @IncorrectQuestions = (@[email protected]) 

Select @CorrectQuestions as 'WinningAnswers', 
@TotalQuestions as 'TotalAnswers', 
@IncorrectQuestions as 'IncorrectQuestions' 
+0

我認爲這是正確的答案..一半測試它.. – WithFlyingColors