如何使用Mysql和Php从这三个表生成学生的报告卡

时间:2013-08-07 11:54:30

标签: php mysql

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

students_info

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

scores_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

NAME KINSLEY         性别M         年龄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

......等所有学生。

你的帮助将非常感激

感谢

2 个答案:

答案 0 :(得分:0)

你是如此亲密。在group by子句中错过了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

FIDDLE

答案 1 :(得分: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代码以查看它将产生的内容。

祝你好运!