简体   繁体   English

如何从codeigniter中的数据库同时从同一列获取最小值和最大值?

[英]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. 我想在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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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