1

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

在此处输入图像描述

现在我想在单页中显示所有学生的成绩单,如下所示:

在此处输入图像描述

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

我正在使用 Codeigniter,请您帮我在我的模型和视图文件中编码什么?

提前致谢 :)

这是我的控制器:

         $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";} ?>

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

在此处输入图像描述

如果你注意到我的表midterm,你会看到那个id3 岁的学生在英语和数学上的分数是一样的。但在我的输出中,它只在中间显示一个主题的标记。是因为您distinct在查询中使用了它而发生的吗?我试过删除不同的,但它没有显示确切的数据。

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

再次感谢您的帮助。:)

4

1 回答 1

6

我已经为您找到了解决方案。它将为每个学生获取一条记录,包含他的所有信息。我这里用过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);

或 Active Record 版本

$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 />'  ;
 }
于 2012-04-17T07:03:41.787 回答