gpt4 book ai didi

php - 如何用Mysql和Php从这三个表为学生生成成绩单

转载 作者:行者123 更新时间:2023-11-30 01:25:00 24 4
gpt4 key购买 nike

我有三个链接在一起的表。我想做的是为类(class)中的所有学生生成一份成绩单。

表格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

姓名金斯利 性别男 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

...对于所有学生等等。

我们将不胜感激您的帮助

谢谢

最佳答案

你们离得很近。刚刚在 group by 子句中错过了 student_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

关于php - 如何用Mysql和Php从这三个表为学生生成成绩单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18102724/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com