2011-02-08 70 views
1

我想顯示StudentNo,SubjectCode,SubjectDescription和成績,但是當學生失敗時,它會爲所有字段生成另一個條目,所以我想要顯示studentNo,SubjectCode和SubjectDescription一次,同時顯示2個成績該學生.. 例的特定主題: enter image description here如何查看字段的雙重輸入並僅顯示其他字段的一個輸入項?

我希望僅顯示這樣的:( 07-08-061 EN110 5/1) 這裏是我的代碼:

**

mysql_select_db($database_strawman, $strawman); 
$query_Recordset1 = "SELECT curriculum.SCode, curriculum.SDesc, grade.Grade, students.StudNo FROM students INNER JOIN (curriculum INNER JOIN grade ON curriculum.SCode = grade.GSCode) ON students.StudNo = grade.GStudNo GROUP BY StudNo"; 
$Recordset1 = mysql_query($query_Recordset1, $strawman) or die(mysql_error()); 

$totalRows_Recordset1 = mysql_num_rows($Recordset1); 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Untitled Document</title> 
</head> 

<body> 
<table width="200" border="1"> 
    <tr> 
    <td>Subject Code</td> 
    <td>Subject Description</td> 
    <td>Grade</td> 
    <td>Student Number</td> 
    </tr> 
    <?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1)){ ?> 
    <tr> 

    <td><?php echo $row_Recordset1['SCode']; ?></td> 
    <td><?php echo $row_Recordset1['SDesc']; ?></td> 
    <td><?php echo $row_Recordset1['Grade']; ?></td> 
    <td><?php echo $row_Recordset1['StudNo']; ?></td> 

    </tr> 
    <?php } ?> 


</table>&nbsp;</p> 
</body> 
</html> 
<?php 
mysql_free_result($Recordset1); 
?> 
** 

回答

0

爲您彙總功能嘗試使用GROUP_CONCAT代替SUM

SELECT c.SCode, c.SDesc, GROUP_CONCAT(g.Grade), s.StudNo 
    FROM students s 
     INNER JOIN grade g 
      ON s.StudNo = g.GStudNo 
     INNER JOIN curriculum c 
      ON g.GSCode = c.SCode 
    GROUP BY c.SCode, c.SDesc, s.StudNo 
0

,你可以添加一個where子句 像

where grade > 1 

(插入最小等級通過)

+0

現在看來,這會只顯示大於1的等級。但也想打印出通過等級的失敗等級..所以輸出將是一個科目的2個等級.. – PiDO 2011-02-08 15:46:52

0

集團通過學號和密碼,使用GROUP_CONCAT得到羅列出來的2(+)等級分組。

SELECT *, group_concat(grade) as grades FROM students GROUP BY GSTudNo, GSCode 

那是基礎,使用您的查詢它會是這樣的:

SELECT curriculum.SCode, curriculum.SDesc, SUM(grade.Grade) grade_total, students.StudNo, group_concat (grade.Grade) as grades 
FROM students 
INNER JOIN (curriculum INNER JOIN grade ON curriculum.SCode = grade.GSCode) 
ON students.StudNo = grade.GStudNo 
GROUP BY StudNo, GSCode 
+0

我怎麼能在加入聲明中使用它? – PiDO 2011-02-08 15:47:51

+0

@PiDO我編輯了我的答案以使用您的查詢。你也將不得不改變它所說的PHP echo $ row_Recordset1 ['Grade'];說echo $ row_Recordset1 ['grades']; – profitphp 2011-02-08 16:05:00

相關問題