0

Get maximum value of row in mysql using codeigniter.
i have following mysql data enter image description here

I need to get the details based on total_payment of each student. like thisenter image description here

i tried code below

 $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();
      }
9
  • looking at your desire result you are searching for minimum balance Commented Mar 31, 2017 at 6:36
  • am I right ?... Commented Mar 31, 2017 at 6:37
  • visit stackoverflow.com/questions/16538359/… Commented Mar 31, 2017 at 6:37
  • @knowledge.... get last payment where each student and each fee_category Commented Mar 31, 2017 at 6:45
  • So you can say this if student pay fee it has lowest balance right ? Commented Mar 31, 2017 at 6:48

1 Answer 1

0

try this

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.