0

我有三个链接在一起的表。我想做的是为一个班级的所有学生生成一张成绩单。

学生信息

name      sex age students_ID
--------- --- --- -----------
Kinsley   M    12           1
Michael   m    12           2
Rhianna   f    22           3

score_panel

1stCA 2ndCA exam students_ID subjectID
----- ----- ---- ----------- ---------
   23    15   42           1         1
   10    12    7           1         2
   43    15   62           1         3
   10    12   27           2         1
   10    12   57           2         2
   23    15   12           2         3
   11    12   27           3         1
   04    12   57           3         2
   13    25   12           3         3

主题

subjectname subjectID
----------- ---------
english             1
maths               2
biology             3

我希望我的结果看起来像这样:

NAME KINSLEY
SEX M
AGE 12

和成绩单跟随

subject   1stCA 2ndCA EXAM
--------- ----- ----- ----
english      23    15   42
maths        10    12    7
Biology      43    15   62

...等等所有学生

只检索一门科目和分数,而不是全部

<?php
include("connect.php");

$generate="SELECT students_info.name, subject.subjectname, scores_panel.1stCA, scores_panel.2ndCA, scores_panel.EXAM
FROM
students_info
LEFT JOIN
scores_panel
ON students_info.students_ID=scores_panel.students_ID
LEFT JOIN
subject
ON
subject.subjectID=scores_panel.subjectID
GROUP BY scores_panel.subjectID  ";

$fetch=mysql_query($generate);
while($row=mysql_fetch_array($fetch)or die(mysql_error()))
{
?>
**NAME:** 
<?PHP echo $row['name']; ?>
subject 1stCA 2ndCA EXAM
----------
<?PHP echo $row['subjectname']; ?>
<?PHP echo $row['1stCA']; ?>     
<?PHP echo $row['2ndCA']; ?>   
<?PHP echo $row['EXAM']; ?>

THIS IS YOUR REPORT CARD 
<?PHP } ?>

它有效,但每个学生只显示一个主题,

例子

  NAME Rhianna 
    SEX F
    AGE 22

和成绩单跟随

subject   1stCA 2ndCA EXAM
--------- ----- ----- ----
english      11    12   27

姓名 KINSLEY 性别 12 岁

和成绩单跟随

subject   1stCA 2ndCA EXAM
--------- ----- ----- ----
english      23    15   42

而不是这样的:

  NAME KINSLEY
    SEX M
    AGE 12

和成绩单跟随

subject   1stCA 2ndCA EXAM
--------- ----- ----- ----
english      23    15   42
maths        10    12    7
Biology      43    15   62


  NAME Rhianna 
    SEX F
    AGE 22

和成绩单跟随

subject   1stCA 2ndCA EXAM
--------- ----- ----- ----
english      11    12   27
maths        04    12    57
Biology      13    25   12

...等等所有学生。

您的帮助将不胜感激

谢谢

4

2 回答 2

0

在获取和显示数据的过程中存在一些缺陷。似乎主要缺陷是限制性的“GROUP BY”语句。有多种解决方案,但我将展示一个简单的解决方案,它只需要执行一个查询。

实际上,您的过程包含两个循环:一个是您获取学生的循环,另一个是您想要结果的循环。它是这样的:

<?php
include("connect.php");

$generate="SELECT students_info.students_ID, students_info.name, subject.subjectname, scores_panel.1stCA, scores_panel.2ndCA, scores_panel.EXAM
FROM
students_info
LEFT JOIN
scores_panel
ON students_info.students_ID=scores_panel.students_ID
LEFT JOIN
subject
ON
subject.subjectID=scores_panel.subjectID
ORDER BY students_info.name ASC
";

$fetch=mysql_query($generate);
$previousId = -1;
while($row=mysql_fetch_array($fetch)or die(mysql_error()))
{
    if($row['students_ID'] != $previousId)
    {
        //New student: show name [and other info]
        echo "**" . $row['name'] . "**";
        echo "subject 1stCA 2ndCA EXAM";
        echo "------------------------";

        $previousId = $row['students_ID'];
    }
    ?>
<?PHP echo $row['subjectname']; ?>
<?PHP echo $row['1stCA']; ?>     
<?PHP echo $row['2ndCA']; ?>   
<?PHP echo $row['EXAM']; ?>

THIS IS YOUR REPORT CARD
<?php
}
?>

发生了什么?SQL 输出的行都包含一个学生 ID 和他们的个性。它只需要显示一次(与表格标题一起),即当一个新学生出现在输出中时。该变量$previousId用于此目的。然后所有数据都显示在该成绩表中。

如果您熟悉 phpMyAdmin,您可以随时测试您的 SQL 代码,看看它会产生什么。

祝你好运!

于 2013-08-07T12:22:01.117 回答
0

你是如此接近。只是students_info.students_ID被克劳斯分组错过

SELECT students_info.name, subject.subjectname, scores_panel.1stCA, scores_panel.2ndCA, scores_panel.EXAM
FROM
students_info
LEFT JOIN
scores_panel
ON students_info.students_ID=scores_panel.students_ID
LEFT JOIN
subject
ON
subject.subjectID=scores_panel.subjectID
GROUP BY students_info.students_ID,scores_panel.subjectID

小提琴

于 2013-08-07T12:11:40.383 回答