简体   繁体   中英

How to get min and max value from the same column at same time from database in codeigniter?

I want to get the minimum and maximum values, at the same time, with one query in CodeIgniter.

Here is my code snippet:

$this->db->select_min('table.col1');
$this->db->select_max('table.col1');
$this->db->select('table.col2');
$this->db->from('atm_rates');
$this->db->order_by('table.col2');
$query = $this->db->get();
return $query->result_array();

You can try this select:

$this->db->select('MAX(col1), MIN(col1)'); $query = $this->db->get('table');

OR

$sql = "SELECT MAX(col1) AS max, MIN(col1) as min FROM table; $query = $this->db->query($sql)->get();

And then

return $query->result_array();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM