2015-05-16 36 views
1

我正在嘗試使用下面的查詢從表中選擇數據。WHERE條件不能與聚合函數一起工作

我的表: enter image description here

SELECT COUNT(*) AS totalcount, 
     SUM(English) as EnglishTotal, 
     SUM(Tamil) as TamilTotal, 
     SUM(Maths) as MathsTotal, 
     SUM(EVS) as EVSTotal, 
     SUM(Science) as ScienceTotal 
    from StudentMarks 
where Class='"+Classs+"' and Section='"+Section+"' and ExamType='"+Exam+"'; 

如果我檢查上面ResultSet像下面的查詢,

if(!rscount.isBeforeFirst()){ 
    System.out.println("Cominggggg iffff"); 
} 
else{ 
    System.out.println("Cominggggg elsee"); 
} 

條件總是轉到其他部分甚至WHERE條件不滿足。

注意:如果我將我的查詢更改爲SELECT * from StudentMarks where Class='"+Classs+"' and Section='"+Section+"' and ExamType='"+Exam+"';,那麼它工作正常。

有人能指導我,我在哪裏做錯了嗎?

+0

在where子句之後使用group by子句.i.e GROUP BY(Sno) –

+0

請參閱規範化。 RDBMS表格不是電子表格 – Strawberry

回答

3

請嘗試以下查詢。

SELECT COUNT(*) AS totalcount,SUM(English) as EnglishTotal, 
SUM(Tamil) as TamilTotal,SUM(Maths) as MathsTotal, 
SUM(EVS) as EVSTotal,SUM(Science) as ScienceTotal 
from StudentMarks where Class='"+Classs+"' 
and Section='"+Section+"' and ExamType='"+Exam+"' GROUP BY Section; 

我已經在條件後添加了group by子句。

+0

是它的工作。 –