2012-11-15 85 views
0

我運行一個查詢:查詢與聯接

select course.course,iars.id, 
     students.rollno, 
     students.name as name, 
     teachers.name as tname, 
     students.studentid, 
     attndata.studentid ,sum(attndata.obt) as obt 
     sum(attndata.benefits) as ben , (sum(attndata.max)) as abc 
from groups, students 
     left join iars 
      on iars.id 
     left join str 
      on str.studentid=students.studentid 
     left join course 
      on course.c_id=students.course 
     left join teachers 
      on teachers.id=iars.teacherid 
     join sgm 
      on sgm.studentid=students.studentid 
     left join attndata 
      on attndata.studentid=students.studentid and iars.id=attndata.iarsid 
     left join sps 
      on sps.studentid=students.studentid and iars.paperid=sps.paperid 
     left join semdef 
      on semdef.semesterid=str.semesterid 
     where students.course='1' 
      and students.status='regular' 
      and sps.paperid='5' 
      and iars.courseid=students.course 
      and iars.semester=str.semesterid 
      and semdef.month=9 
      and iars.paperid='5' 
      and str.semesterid='1' 
      and str.sessionid='12' 
      and groups.id=sgm.groupid 
     group by sps.studentid, 
      teachers.id, 
      semdef.month 
     order by 
      students.name 

在此查詢,每當我有左連接上semdef.id=attndata.mon,我得到結果爲零時的semdef.id=null的價值,但我希望所有的結果,無論semdef,但我想用它。因爲它應該獲取結果,如果值爲空。你能幫忙嗎?

回答

2

這可能是因爲你的where子句是說

and semdef.month=9 

,你可能想

and (semdef.month=9 OR semdef.id IS NULL) 

或類似的東西。

+0

好的,如果我有一些值爲空,我怎麼能顯示他們爲零從查詢本身? –

+0

嘗試「ISNULL(yourfield,0)」而不是「yourfield」 – MikeSmithDev

1

這是因爲你的where子句有關於semdef表的語句。將這些添加到join子句中,將它們放在暗示內連接的位置。

如:

Left join semdef on xxx and semdef.id = attndata.min