Codeigniter:自定义排序查询


Codeigniter: Customize ordering query

我想实现做ORDER BY task_status = 'Open'但是我无法得到结果。

我做了这个

$this->db->from('session_tasks');
$this->db->order_by('task_status', 'OPEN', 'DESC');        
$query = $this->db->get();

我希望任何人都能帮忙。

试试这个,

 $this->db->query("SELECT * FROM session_tasks
         ORDER BY task_status = 'OPEN' DESC,task_status ASC"); 

或者

$this->db->query("SELECT * FROM session_tasks
         ORDER BY CASE WHEN task_status = 'OPEN' THEN 0 ELSE 1 END ASC,
         task_status ASC"); //Ordering starts with Open, then in Ascending order

这里是使用Codeigniter活动记录的另一个解决方案。注意在使用字符串字面值时使用双引号。

$this->db->_protect_identifiers = FALSE;
$this->db->from('session_tasks');
$this->db->order_by("task_status = 'OPEN'", 'DESC');
$this->db->order_by('task_status');
$query = $this->db->get();
$this->db->_protect_identifiers = TRUE;

使用此代码

$this->db->order_by("task_status", "desc");
$query = $this->db->get_where('session_tasks', array('task_status'=>'open'));

查看文档点击这里

你可以这样做:

// set this to false so that _protect_identifiers skips escaping:
$this->db->_protect_identifiers = FALSE;
// your order_by line:
$this -> db -> order_by('FIELD ( products.country_id, 2, 0, 1 )');
 // important to set this back to TRUE or ALL of your queries from now on will be non-escaped:
$this->db->_protect_identifiers = TRUE;