How to get max and min value of a column from database in codeigniter?

Member

by mac , in category: PHP Frameworks , a year ago

How to get max and min value of a column from database in codeigniter?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by rollin , a year ago

@mac 

To get the maximum and minimum value of a column from a database in CodeIgniter, you can use the following methods:

  1. Using Active Record:


To get the maximum value of a column:

1
2
3
$this->db->select_max('column_name');
$query = $this->db->get('table_name');
$max_value = $query->row()->column_name;


To get the minimum value of a column:

1
2
3
$this->db->select_min('column_name');
$query = $this->db->get('table_name');
$min_value = $query->row()->column_name;


  1. Using Query Builder:


To get the maximum value of a column:

1
2
3
$this->db->select_max('column_name');
$query = $this->db->get('table_name');
$max_value = $query->row()->column_name;


To get the minimum value of a column:

1
2
3
$this->db->select_min('column_name');
$query = $this->db->get('table_name');
$min_value = $query->row()->column_name;


Make sure to replace 'column_name' with the actual name of the column you want to get the maximum and minimum values from, and 'table_name' with the actual name of the table in your database.

Related Threads:

How get data from database min to max in laravel?
How to get the min/max axis values in chart.js?
How to get the max value from count value in sql oracle?
How to search between min and max in laravel?
How to get value from another model in codeigniter?
How to make a search query for max and min in symfony 4?