Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 具有sum函数时查询运行不正常_Mysql_Sql_Group By - Fatal编程技术网

Mysql 具有sum函数时查询运行不正常

Mysql 具有sum函数时查询运行不正常,mysql,sql,group-by,Mysql,Sql,Group By,我正在运行一个查询: select iars.id,students.rollno, students.name as name, teachers.name as tname, students.studentid, t1.studentid, sum(t1.obt) as obt1, sum(t1.benefits) as ben1, sum(t1.max) as max1, (t2.obt) as obt2, (t2.ben

我正在运行一个查询:

select 
    iars.id,students.rollno,
    students.name as name, 
    teachers.name as tname, 
    students.studentid, 
    t1.studentid,
    sum(t1.obt) as obt1, sum(t1.benefits) as ben1, sum(t1.max)  as max1,
    (t2.obt) as obt2, (t2.benefits) as ben2, (t2.max)  as max2,
    (t3.obt) as obt3, (t3.benefits) as ben3, (t3.max)  as max3,
    (t4.obt) as obt4, (t4.benefits) as ben4, (t4.max)  as max4,
    (t5.obt) as obt5, (t5.benefits) as ben5, (t5.max)  as max5 
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 semdef on month
left join sps on sps.studentid=students.studentid and iars.paperid=sps.paperid
left join `attndata` `t1` on ((t1.studentid=students.studentid) and iars.id=t1.iarsid and t1.mon=1)
left join `attndata` `t2` on ((t1.studentid=t2.studentid) and t2.mon = 2 and iars.id=t2.iarsid)
left join `attndata` `t3` on ((t2.studentid=t3.studentid) and t3.mon = 4 and iars.id=t3.iarsid)
left join `attndata` `t4` on ((t3.studentid=t4.studentid) and t4.mon = 5 and iars.id=t4.iarsid)
left join `attndata` `t5` on ((t4.studentid=t5.studentid) and t5.mon = 6 and iars.id=t5.iarsid)
where students.course='1' and students.status='regular' and sps.paperid='2' and 
iars.courseid=students.course and iars.semester=str.semesterid and iars.paperid='2' and
str.semesterid='1' and str.sessionid='12' and groups.id=sgm.groupid 
group by sps.studentid , teachers.id order by students.name

这是一种分组,就像我想按月份求和一样,但每当我使用sum时,它都会给我一个有趣的结果。

如果你想按月份求和,你应该按日期和月份分组。与学生和老师分组每月会给您多行,并会产生有趣的结果

当然,按日期分组,教师分组,或按教师分组,日期取决于您的基本报告需要。我将其更改为“教师”和“日期”,但仍然无法正确显示。请编辑问题。展示你想要得到的和你正在得到的。