Mysql 带联接的查询

Mysql 带联接的查询,mysql,sql,join,group-by,Mysql,Sql,Join,Group By,我正在运行一个查询: 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

我正在运行一个查询:

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=attnda.mon
上离开join时,当
semdef.id=null
的值时,我得到的结果为零,但我想要所有结果,不管
semdef
,但我想要使用它。如中所示,如果值为null,则应获取结果。您能帮忙吗。

这是因为您的where子句包含与semdef表相关的语句。将它们添加到join子句中,因为将它们放在where中意味着内部联接

例如:


这是因为where子句包含与semdef表相关的语句。将它们添加到join子句中,因为将它们放在where中意味着内部联接

例如:


可能是因为你的where子句

and semdef.month=9
你可能想要

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

或者类似的东西。

可能是因为您的where子句

and semdef.month=9
你可能想要

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

或者类似的东西。

好的,如果我有一些值为null,如何从查询本身将它们显示为零?尝试“ISNULL(yourfield,0)”而不是“yourfield”好的,如果我有一些值为null,如何从查询本身将它们显示为零?尝试“ISNULL(yourfield,0)”而不是“yourfield”