使用codeigniter获取mysql中行的最大值

时间:2017-03-31 06:32:32

标签: mysql codeigniter

使用codeigniter获取mysql中行的最大值 我有以下mysql数据enter image description here

我需要根据每个学生的total_payment获取详细信息。 像这样enter image description here

我尝试了以下代码

 $student_id_dis = $this->db->query('SELECT DISTINCT(student_id) FROM student_fees')->result_array();
 $fee_cat_id_dis = $this->db->query('SELECT DISTINCT(fee_category_id) FROM student_fees')->result_array();
 $this->db->select(['student_fees.*', 'fee_categories.fee_category_name as fee_name', 'fee_categories.amount as fee_amount']);

 $this->db->join('student', 'student_fees.student_id = student.student_id');
 $this->db->join('fee_categories', 'student_fees.fee_category_id = fee_categories.fee_categories_id');

 $where = '';

 for ($i = 0; $i < count($student_id_dis); $i++) {
    if (isset($fee_cat_id_dis[$i]['fee_category_id'])) {
        $where .='total_paid = (SELECT max(stdp.total_paid)
                                   FROM student_fees stdp
                                   WHERE stdp.fee_category_id = ' . $fee_cat_id_dis[$i]['fee_category_id'] . ')';
     }

     $this->db->where($where);

     $this->db->get('student_fees')->result_array();
      }

1 个答案:

答案 0 :(得分:0)

试试这个

select * from student_fees where student_fees_id in 
(select student_fees_id from 
  where total_paid =max(total_paid) group by fee_category_id)