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'
它的查询工作正常。但是如果主题值为null,则所有学生记录都不会显示在该主题中。 我需要更多的发烧。
答案 0 :(得分: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;
}