0
SELECT * FROM ( 
select student_id, class_id,  
       sum(number*(1-abs(sign(subject-1)))) as sub1,  
       sum(number*(1-abs(sign(subject-2)))) as sub2,  
       sum(number*(1-abs(sign(subject-3)))) as sub3,  
       sum(number*(1-abs(sign(subject-4)))) as sub4,  
       sum(number*(1-abs(sign(subject-5)))) as sub5,  
       sum(number*(1-abs(sign(subject-6)))) as sub6  
from result GROUP BY student_id,class_id) m 
where class_id = '7'

它的查询工作正常。但如果主题值为空,则所有学生记录都不会显示在该主题中。我需要再发烧一次。

4

1 回答 1

2

不知道有没有直接使用mysql的方法,但是我可以建议你使用php

$sql = mysql_query("SELECT m.* FROM (  
select student_id, class_id,   
       sum(number*(1-abs(sign(subject-1)))) as sub1,   
       sum(number*(1-abs(sign(subject-2)))) as sub2,   
       sum(number*(1-abs(sign(subject-3)))) as sub3,   
       sum(number*(1-abs(sign(subject-4)))) as sub4,   
       sum(number*(1-abs(sign(subject-5)))) as sub5,   
       sum(number*(1-abs(sign(subject-6)))) as sub6   
from result GROUP BY student_id,class_id) m  
where m.class_id = '7'");

$row = mysql_fetch_array($sql);
$sum = 0;
for($i = 1; $i < count($row); $i++) {
    $sum += isset($row['sub' . $i]) ? $row['sub' . $i] : 0;
}
于 2012-08-08T04:40:28.780 回答