- Seme Framework
- Credits
- Version 3.2.X
- Issue
- Deployment
Union Order By Method
The union_order_by
method is part of database class builder for sorting result query.
Basic Usage
Here is the basic usage union_order_by
method from $db
property on SENE_Model class.
$this->db->group_by(string $column_name, string $sort_direction): $this->db
Parameters
This method has 2 required parameters.
$column_name
The $column_name value can be filled by column name or function string.
$sort_direction
The $sort_direction value can be string like:
asc
for ascending or,desc
for descending
Example
For example we assumed want to retrieve newest articles from blog table.
<?php
class Blog_Model extends SENE_Model{
var $tbl = 'd_blog';
var $tbl_as = 'b';
public function __construct(){
parent::__construct();
$this->db->from($this->tbl,$this->tbl_as);
}
public function searchByScore($keyword){
$this->db->union_flush();
//1st union
$this->db->select('id');
$this->db->select('title');
$this->db->select('excerpt');
$this->db->select('date_modified');
$this->db->select_as("3",'score');
$this->db->from($this->tbl,$this->tbl_as);
$this->db->where('title',$keyword,'or','like',1,0);
$this->db->where('excerpt',$keyword,'or','like',0,1);
$this->db->union_create();
//2nd union
$this->db->select('id');
$this->db->select('title');
$this->db->select('excerpt');
$this->db->select('date_modified');
$this->db->select_as("2",'score');
$this->db->from($this->tbl,$this->tbl_as);
$this->db->where('title',$keyword,'or','like%',1,0);
$this->db->where('excerpt',$keyword,'or','like%',0,1);
$this->db->union_create();
//2nd union
$this->db->select('id');
$this->db->select('title');
$this->db->select('excerpt');
$this->db->select('date_modified');
$this->db->select_as("1",'score');
$this->db->from($this->tbl,$this->tbl_as);
$this->db->where('title',$keyword,'or','%like',1,0);
$this->db->where('excerpt',$keyword,'or','%like',0,1);
$this->db->union_create();
//3nd union
$this->db->select('id');
$this->db->select('title');
$this->db->select('excerpt');
$this->db->select('date_modified');
$this->db->select_as("0",'score');
$this->db->from($this->tbl,$this->tbl_as);
$this->db->where('title',$keyword,'or','%like%',1,0);
$this->db->where('excerpt',$keyword,'or','%like%',0,1);
$this->db->union_create();
$this->db->union_select('id,title,date_modified,score','score');
$this->db->union_group_by('id');
$this->db->union_order_by('score','desc')->union_limit(1, 14);
return $this->db->union_get();
}
}