2015-08-27 1 views
0

Je veux atteindre ORDER BY task_status = 'Open' mais je n'arrive pas à obtenir le résultat.Codeigniter: Personnaliser demande de commande

Je l'ai fait

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

J'espère que quelqu'un peut aider.

+0

Quelles sont les options disponibles dans 'task_status'? –

+0

@RohanKumar il y a Fermer, Ouvert, Payé, En attente –

Répondre

2

Essayez ce,

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

Alternativement,

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

Une solution plus je pense, mais je havn't essayé

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

cela a résolu le problème –

0

utiliser ce code

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

pour vérifier la documentation click here

+0

Maintenant, je ne peux pas obtenir les autres lignes où son 'task_status! = Open' -.- –

+0

utiliser 1,2,3 dans votre db pour task_status et supprimer où condition ... donne 1 à 'open' et à order $ this-> db-> order_by ("task_status", "asc"); – parth

0

Vous pouvez le faire comme ceci:

// 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; 

Source