【问题标题】:Sort total scores from highest to lowest总分从高到低排序
【发布时间】: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


【解决方案1】:

您可以在查询中添加order by 语句

$this-&gt;db-&gt;order_by( $totField , 'DESC');

DESC 用于降序 或ASC 用于升序

已编辑

这样的事情怎么样:

$this->db->select( SUM($totField) );
$this->db->group_by( 'student_id', 'subject_id' );
$this->db->order_by( $totField , 'DESC');
$q = $this->db->get($table);

结果将是一个数组,其中包含每对学生/科目的总分,并按总分排序。

【讨论】:

  • 我明白,但这会尝试对每个科目的总分进行排序。不是总分。
  • 哦,好的,查询只返回一个学生和一个科目的分数!为什么不在一个查询中返回所有内容,直接将所有分数与group by subject_id, student_id 相加?
  • 我已经修改了帖子。请帮我检查查询
  • @Mary4bills 我编辑了我的答案。这个想法是在一个查询中获取每一对学生/主题,而不是从多个查询中一个一个地获取它们
  • 我认为我们已经很接近了。您的查询获得了最高分,但现在,最高分出现在所有列中,包括主题列。
猜你喜欢
  • 2020-08-10
  • 1970-01-01
  • 1970-01-01
  • 2020-11-12
  • 2017-02-06
  • 1970-01-01
  • 1970-01-01
  • 2015-04-13
  • 1970-01-01
相关资源
最近更新 更多