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

Member

by mac , in category: PHP Frameworks , 8 months 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 , 8 months 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.