【问题标题】:How to find highest average score in a class如何找到班级中的最高平均分
【发布时间】:2020-12-17 16:38:09
【问题描述】:

我正在尝试显示班级的最高平均分;但是,我一直在与查询作斗争几个小时,似乎无法让它工作。

我可以获得要显示的学生平均成绩列表,其中:

   public function GetSumAvgftScore($student_id, $session_id, $section_id, $class_id) 
    {
        $this->db->select_avg('ft_tot_score');
        $this->db->where('student_id', $student_id);
        $this->db->where('session_id', $session_id);
        $this->db->where('section_id', $section_id);
        $this->db->where('class_id', $class_id);
        return $this->db->get('ftscores_primary')->row(); 
    }

但是,当我尝试显示最高平均分时,我遇到了困难。这是我尝试过的:

  public function GetHighestClassAvg($student_id, $session_id, $section_id, $class_id) 
    {
        $this->db->order_by('ft_tot_score', 'DESC');
        $this->db->select_max(avg('ft_tot_score'));
        $this->db->where('student_id', $student_id);
        $this->db->where('session_id', $session_id);
        $this->db->where('section_id', $section_id);
        $this->db->where('class_id', $class_id);
        $data = $this->db->get('ftscores_primary')->row(); 
    }

这并没有给我任何东西。

【问题讨论】:

  • 这个$this->db->where('student_id', $session_id); 是您的问题中的拼写错误还是您的问题的可能原因?
  • 我已经纠正了,现在我得到了错误Message: Call to undefined function avg()

标签: php mysql codeigniter


【解决方案1】:

试试这个:

 function GetHighestClassAvg($student_id, $session_id, $section_id, $class_id) 
    {
        $this->db->order_by('ft_tot_score', 'DESC');
        $this->db->select_avg('ft_tot_score');
        $this->db->where('student_id', $student_id);
        $this->db->where('session_id', $session_id);
        $this->db->where('section_id', $section_id);
        $this->db->where('class_id', $class_id);
        $data = $this->db->get('ftscores_primary')->row(); 
    }

【讨论】:

  • 这仍然是给我学生的平均分。我想挑出最高的平均值。
  • 平均值会是同一个数,最大值是多少
  • 要显示在学生成绩单上。学生的平均分会显示在他的工作表上,该班级的最高平均分也会显示在他的工作表上。
  • 在这种情况下,从给定查询的结果中选择 Max。使用 LIMIT $this->db->limit(1); $query = $this->db->get('my_table'); $myRow = $query->row();使用 OFFSET 和 LIMIT $query = $this->db->get('mytable', 0, 1); $myRow = $query->row();
  • 不幸的是,它并没有给我最高的平均值
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-06-05
  • 2015-01-17
  • 2022-01-10
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多