codeigniter 如何将两个独立的数据合并为一个值?

vojdkbi0  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(134)

我试图显示学生的成绩,预赛,期中和期末考试。
问题是年级被加倍与单独的年级期间的成绩。
此图显示了问题:

**预期输出:**是否有一种方法可以像此图像一样将成绩连接在一起?

查看方式:

<?php foreach($grades as $grade): ?>
<tr>
    <th scope="row"><?php echo $grade['subCode']; ?></th>
    <td><?php echo $grade['subject']; ?></td>
        <?php if($grade['gradePeriod']=="Prelim"): ?>
            <td><?php echo $grade['grade']; ?></td>
        <?php else: ?>
            <td>0</td>
        <?php endif; ?>

        <?php if($grade['gradePeriod']=="Midterm"): ?>
            <td><?php echo $grade['grade']; ?></td>
        <?php else: ?>
            <td>0</td>
        <?php endif; ?>

        <?php if($grade['gradePeriod']=="Finals"): ?>
            <td><?php echo $grade['grade']; ?></td>
        <?php else: ?>
            <td>0</td>
        <?php endif; ?>

    <td><?php echo $grade['remarks']; ?></td>
</tr>
<?php endforeach; ?>

产品型号:

public function gradePeriod(){
    $this->db->select('subCode,subject,grade,gradePeriod,remarks');
    $this->db->from('tbl_college_grades');
    $this->db->where('studentID', ' 200171419');
    $this->db->where('schoolYear','2022-2023');
    $this->db->where('semester','First Semester');
    $query = $this->db->get();
    return $query->result_array();

}
euoag5mw

euoag5mw1#

基于此MySQL - Rows to Columns
我假设您的表值如下所示:
| 子代码|科目|坡度|年级周期|备注|
| - -|- -|- -|- -|- -|
| 不良事件7|会计实习|八十二|初步|已通过|
| 不良事件7|会计实习|九十六|期中考试|已通过|
| 不良事件7|会计实习|九十九|总决赛|已通过|
您想要将ROW转换为COLUMN
| 子代码|科目|初步|期中考试|总决赛|备注|
| - -|- -|- -|- -|- -|- -|
| 不良事件7|会计实习|八十二|九十六|九十九|已通过|

步骤1:在数据库中创建视图

create view grade_extended as (
  select
    subCode,
    subject,
    grade,
    gradePeriod,
    case when gradePeriod = "Prelim" then grade end as Prelim,
    case when gradePeriod = "Prelim" then remarks end as Prelim_remarks,
    case when gradePeriod = "Midterm" then grade end as Midterm,
    case when gradePeriod = "Midterm" then remarks end as Midterm_remarks,
    case when gradePeriod = "Finals" then grade end as Finals,
    case when gradePeriod = "Finals" then remarks end as Finals_remarks,
    remarks
  from tbl_college_grades
);

步骤2:创建数据透视表视图

create view grade_pivot as (
  select
    subCode,
    subject,
    sum(Prelim) as Prelim,
    group_concat(Prelim_remarks) as Prelim_remarks,
    sum(Midterm) as Midterm,
    group_concat(Midterm_remarks) as Midterm_remarks,
    sum(Finals) as Finals,
    group_concat(Finals_remarks) as Finals_remarks
  from grade_extended
  group by subCode, subject
);

步骤3:选择视图

select * from grade_pivot

相关问题