2016-01-01 173 views
0

我對SQL和編碼一般都很陌生。使用SQL,如何計算查詢結果的數量?

我有一個SQL查詢,工作正常。我現在要做的就是返回查詢結果中的行數。

當前的SQL查詢:

SELECT 
    Progress.UserID, Questions.[Question Location], 
    Questions.[Correct Answer], Questions.[False Answer 1], 
    Questions.[False Answer 2], Questions.[False Answer 3] 
FROM 
    Questions 
INNER JOIN 
    Progress ON Questions.[QuestionID] = Progress.[QuestionID] 
WHERE 
    (((Progress.UserID) = 1) AND 
    ((Progress.Status) <> "Correct") 
    ); 

我知道我需要使用

SELECT COUNT(*) 

...雖然不能肯定我如何融入查詢。

然後我打算使用OLEDB將結果返回到VB Windows窗體應用程序。

非常感謝所有幫助。

謝謝!喬

+0

'(Progress.Status)<> 「正確的」'應該是'(Progress.Status)<>「Correct'' –

+0

呀,剛剛意識到,無論是它在vis studio中感到困惑。謝謝! –

回答

0

一個簡單的方法是使用子查詢:

select count(*) 
from (<your query here>) as q; 

在你的情況,你也可以改變select是:

select count(*) 

但不會對聚集查詢工作。

+0

您好,感謝您的回覆。 「as q」是什麼意思?謝謝 –

+0

不用擔心,因爲它的作品!非常感謝,真的會幫助我完成計算機課程。 –

+0

SELECT Count(*)AS Expr1 FROM(SELECT Progress.UserID,Questions。[Question Location],Questions。[Correct Answer],Questions。[False Answer 1],Questions。[False Answer 2],Questions。[False ((Progress.UserID)= 1)AND((Progress.Status)<>「Correct」)))AS q; –

1

要計算所有記錄,請使用簡單的子查詢;子查詢必須有別名(這裏我已經命名了子查詢的子查詢)。從基於SQL的人

SELECT COUNT(*) 
FROM (
    SELECT Progress.UserID, Questions.[Question Location],Questions.[Correct Answer], Questions.[False Answer 1], 
    Questions.[False Answer 2], Questions.[False Answer 3] 
    FROM Questions 
    INNER JOIN Progress ON Questions.[QuestionID] = Progress.[QuestionID] 
    WHERE (((Progress.UserID)=1) AND ((Progress.Status)<>"Correct")) 
) AS subquery; 
+0

這很好,雖然我會將COUNT(*)更改爲COUNT(1)。 –

+1

@勞倫斯約翰遜絕對沒關係http://stackoverflow.com/questions/1221559/count-vs-count1 –

+0

這就是我聽DBA得到的結果。感謝您的鏈接。 –

0

完全不同的方法是,一旦他們回到您的應用程序來計算的行 - 你說你正在使用VB,所以你可以很好地使用數據集來保存您的查詢結果。如果是這樣,那麼你只需要這段代碼:

dim rowcount as integer = mydataset.mytable.rows.count 
相關問題