gpt4 book ai didi

mysql - Codeigniter:从三个表生成学生成绩单

转载 作者:行者123 更新时间:2023-11-29 04:11:20 24 4
gpt4 key购买 nike

我的数据库中有以下表格。 midtermfinalterm 表包含学生的分数。

enter image description here

现在我想在单个页面中显示所有学生的评分表,如下所示:

enter image description here

我知道如何在单个表中查询,但在这种情况下我遇到了问题,因为有三个表。

我正在使用 Codeigniter,您能帮我在我的模型和 View 文件中编码什么吗?

提前致谢:)

这是我的 Controller :

         $this->load->model('Mod_student');
$data['records1']= $this->Mod_student->check1();
$this->load->view('view_student',$data);

更新部分

这是我的观点:

    <?php if(count($records1) > 0) { ?>

<?php foreach ($records1 as $row){ ?>



<table>
<tr> <td><?php echo $row['StudentName']; ?></td></tr>

<tr>

<td><?php $midDate = explode(',',$row['MidDate']);
foreach($midDate as $md)
{
echo $md;
echo '<br />' ;
}
?>
</td>

<td><?php $midDate = explode(',',$row['MidSubject']);
foreach($midDate as $md)
{
echo $md;
echo '<br />' ;
}
?>
</td>

<td><?php $midDate = explode(',',$row['MidMarks']);
foreach($midDate as $md)
{
echo $md;
echo '<br />' ;
}
?>
</td>



</tr>





</table>




<?php } ?>

<?php } else { echo "No Record Found";} ?>

我已经更改了我的 View 文件,现在它工作正常 :) 但是我面临着一些问题。如果我运行上面的脚本,我会得到以下输出

enter image description here

如果您注意到我的表格 midterm,您会看到 id 为 3 的学生在英语和数学上取得了相同的分数。但在我的输出中,它只在中间显示一个主题的标记。发生这种情况是因为您在查询中使用了 distinct 吗?我试过删除不同的,但它没有显示准​​确的数据。

你能告诉我如何解决这个问题吗?

再次感谢您的帮助。:)

最佳答案

我已经为您找到了解决方案。它将为每个学生获取包含他所有信息的单个记录。我这里用的是mysql的GROUP_CONCAT。因此,在 php 端你将不得不分解值,然后在循环中访问它。顺便说一句,这里是查询,您可以像这样运行它。

 $query= "select
s.id as SID,
s.name as StudentName,
(select group_concat(distinct midterm.date) from midterm where midterm.student_id = SID) as MidDate,
(select group_concat(distinct midterm.Subject) from midterm where midterm.student_id = SID) as MidSubject,
(select group_concat(midterm.marks) from midterm where midterm.student_id = SID) as MidMarks,
(select group_concat(distinct finalterm.date) from finalterm where finalterm.student_id = SID) as FinalDate,
(select group_concat(distinct finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalMarks,
(select distinct sum(midterm.marks) from midterm where midterm.student_id = SID) as MidTotal,
(select distinct sum(finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalTotal
from students as s
left join midterm as m on m.student_id = s.id
left join finalterm as f on f.student_id = s.id
and f.subject = m.subject
group by s.name;";

$query = $this->db->query($query);

或事件记录版本

$data   =   array(
's.id as SID',
's.name as StudentName',
'(select group_concat(distinct midterm.date) from midterm where midterm.student_id = SID) as MidDate',
'(select group_concat(distinct midterm.Subject) from midterm where midterm.student_id = SID) as MidSubject',
'(select group_concat(midterm.marks) from midterm where midterm.student_id = SID) as MidMarks',
'(select group_concat(distinct finalterm.date) from finalterm where finalterm.student_id = SID) as FinalDate',
'(select group_concat(distinct finalterm.marks) from finalterm where finalterm.student_id = SID) as FinalMarks',
'(select distinct sum(midterm.marks) from midterm where midterm.student_id = SID) as MidTotal',
'(select distinct sum(finalterm.marks)from finalterm where finalterm.student_id = SID) as FinalTotal'
);
$this->db->select($data);
$this->db->group_by('s.name');
$this->db->group_by('s.name');
$this->db->from('students as s');
$this->db->join('midterm as m','m.student_id = s.id','left');
$this->db->join('finalterm as f','f.student_id = s.id','left');
$this->db->group_by('s.name');

具有不同的关键字。这里有一些使用说明。1、每组concat都要展开,在php端使用。像这样

<?php echo $row['StudentName']?>

与Group Concat Item形式查询

 $midDate   =   explode(',',$row['MidDate']);
foreach($midDate as $md)
{
echo $md;
echo '<br />' ;
}

关于mysql - Codeigniter:从三个表生成学生成绩单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10185600/

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