如何查看字段的双重输入并仅显示其他字段的一个条目?

时间:2011-02-08 15:34:19

标签: php mysql join group-by

我希望显示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);
?>
**

3 个答案:

答案 0 :(得分:0)

你可以添加一个where子句 像

where grade > 1 

(插入要通过的最低成绩)

答案 1 :(得分: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

答案 2 :(得分: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