SELECT
A.id_student
,A.sum1_5
,B.sum6_10
FROM
(
SELECT
id_student
,sum(score)as sum1_5
FROM tb_answer
WHERE id_question BETWEEN '1' and '5'
GROUP BY
id_student
) A
LEFT OUTER JOIN(
SELECT
id_question
,sum(score)as sum6_10
FROM tb_answer
WHERE id_question BETWEEN '6' and '10'
GROUP BY
id_student
) B ON A.id_student = B.id_question
<?
include("config.php");
$strSQL= "SELECT A.id_student , A.sum1_5 , B.sum6_10
FROM (SELECT id_student ,sum(score) as sum1_5 FROM tb_answer
WHERE id_question BETWEEN '1' and '5' GROUP BY id_student)
A LEFT OUTER JOIN (SELECT id_question ,sum(score)as sum6_10
FROM tb_answer WHERE id_question BETWEEN '6'and '10'
GROUP BY id_student) B ON A.id_student = B.id_question";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
while($row = mysql_fetch_array($objQuery)){
?>
<table>
<tr>
<td>
รหัสนักเรียน <?=$row['id_student']?>
คะแนนรวม ข้อที่ 1-5 = <?= $row['sum1_5']; ?> คะแนน
</td>
<td>
รหัสนักเรียน <?=$row['id_student']?>
คะแนนรวม ข้อที่ 6-10 = <?= $row['sum6_10']; ?> คะแนน
</td></tr>
<?}?>
</table>
select id_student,group_question,sumscore from
(
select id_student,sum(score) as sumscore,'1-5' as group_question from tb_answer where id_question between 1 and 5 group by id_student
union
select id_student,sum(score) as sumscore,'6-10' as group_question from tb_answer where id_question between 6 and 10 group by id_student
) as a order by id_student,group_question