【发布时间】:2021-03-28 11:21:10
【问题描述】:
我有一张名为“Broadsheet”的表格。这是一个班级学生所有科目总分的汇总。
| sn | name | math | english | french | total |
|---|---|---|---|---|---|
| 1 | mary | 70 | 65 | 85 | 220 |
| 2 | michael | 90 | 70 | 95 | 255 |
| 3 | sam | 80 | 50 | 95 | 225 |
通过此查询获得每个主题的总数
public function getTotalMarksForStudnets($subject_id,$student_id,$table,$totField){
$this->db->select($totField);
$this->db->where('subject_id', $subject_id);
$this->db->where('student_id', $student_id);
$q = $this->db->get($table);
if($q->num_rows() > 0){
return $q->row()->$totField;
}else{
return '0';
}
}
并以此显示
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;">
<?php
$totalMarks = $this->student_model->getTotalMarksForStudnets($tmrow['subject_id'],$stdName['pstudent_id'],$table,$totField);
$gtotal = $gtotal + $totalMarks;
if($totalMarks!=0){
$totSubjects = $totSubjects + 1;
}
echo $totalMarks;
?></td>
虽然用这个来显示总的总数
<td style="border: 1px solid black; font-size:11px;width:120px;text-align:center;"><?php echo $gtotal;?></td>
一切都很顺利,直到我不得不从最高总分到最低分对表格进行排序。 该表现在应如下所示:
| sn | name | math | english | french | total |
|---|---|---|---|---|---|
| 1 | michael | 90 | 70 | 95 | 255 |
| 2 | sam | 80 | 50 | 95 | 225 |
| 3 | mary | 70 | 65 | 85 | 220 |
在这里,我真的卡住了。从这里怎么走?
好的,正如@titi 所建议的,我做了一个 group_by 查询并想出了这个:
public function getOverallMarksForStudnets($subject_id,$student_id,$table,$totField){
$this->db->select_sum($totField);
$this->db->where('subject_id', $subject_id);
$this->db->where('student_id', $student_id);
$this->db->group_by('subject_id', $subject_id);
$this->db->group_by('student_id', $student_id);
$this->db->order_by($totField, 'DESC');
$q = $this->db->get($table);
if($q->num_rows() > 0){
return $q->row()->$totField;
}else{
return '0';
}
}
如果正确,我该如何显示?
使用此查询后,
public function getTotalMarksForStudnets($subject_id,$student_id,$table,$totField){
$this->db->select_sum($totField);
$this->db->group_by( 'student_id', 'subject_id' );
$this->db->order_by( $totField , 'DESC');
$q = $this->db->get($table);
if($q->num_rows() > 0){
return $q->row()->$totField;
}else{
return '0';
}
}
我明白了:
| sn | name | math | english | french | total |
|---|---|---|---|---|---|
| 1 | mary | 255 | 255 | 255 | 255 |
| 2 | michael | 255 | 255 | 255 | 255 |
| 3 | sam | 255 | 255 | 255 | 255 |
我的数据库架构是这样的:
CREATE TABLE `scores_primary` (
`id` int(20) NOT NULL,
`student_id` int(11) DEFAULT NULL,
`class_id` int(5) DEFAULT NULL,
`section_id` int(5) DEFAULT NULL,
`subject_id` int(11) DEFAULT NULL,
`session_id` int(11) DEFAULT NULL,
`ca1` int(11) DEFAULT NULL,
`ca2` int(11) DEFAULT NULL,
`ca3` int(11) DEFAULT NULL,
`ca4` int(11) DEFAULT NULL,
`ca5` float(10,1) DEFAULT NULL,
`ca6` float(10,1) DEFAULT NULL,
`project` int(11) DEFAULT NULL,
`affective` int(11) DEFAULT NULL,
`psychomotor` int(11) DEFAULT NULL,
`exam` int(11) DEFAULT NULL,
`tot_score` int(11) DEFAULT NULL,
`created_at` datetime NOT NULL DEFAULT current_timestamp(),
`modified_at` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
tot_score ($totField) 是在这个宽幅表中收集的各种主题下的内容。 $totalmarks 是所有 tot_score ($totField) 的总和。这就是我想用来对分数进行排序的。
【问题讨论】:
-
所以sn没有意义?
-
我不明白你的意思,但 sn 基本上是对列进行编号
标签: php mysql codeigniter