Group By Method

The group_by method is part of Query Builder method for grouping result set by running GROUP BY SQL command.

Basic Usage

Here is the basic usage group_by method from $db property on SENE_Model class.

$this->db->group_by(string $expression): $this->db

Parameters

This method has 1 required one parameter.

$expression

The $expression value can be single column name or can be SQL Function that compatible with GROUP BY command.

Example

Here is the source code implementation of group_by method in a model class.

class Blog_Model extends \SENE_Model{
  public $tbl = 'blog';
  public $tbl_as = 'b';
  public function __construct(){
    	 parent::__construct();
  }
  public function countByCategory(){
    $this->db->select_as("COUNT(*)",'total',0);
    $this->db->select_as("category",'category',0);
    $this->db->from($this->tbl,$this->tbl_as);
    $this->db->group_by("category");
    return $this->db->get();
  }
}