codeigniter, get the maximum value in mysql table

2019-05-24 10:15发布

问题:

i'm using codeigniter 2.
i've a mysql table column storing the time taken for each student.
eg. 1.2327, 0.6547, 1.9876

i want to get the max. value that column.

This is my code:

$this->db->select_max('time_taken', 'time');
$result = $this->db->get('students');  
echo $result->row()->time;

when i echo the result, it give me a value of 2(correct value should be 1.9876).
What is the correct way to get this value i need, thanks?

回答1:

Try:

$this->db->select_max('time_taken AS time');
$result = $this->db->get('students')->row();  
echo $result->time;

Edit: Make sure your database table field (i.e time_taken) is decimal, NOT integer.



回答2:

$this->db->select_max('time_taken');
$this->db->from('students');
$query=$this->db->get();
return $query->result_array();