Codeigniter : Error in ORDER BY CASE query

前端 未结 5 1402
野性不改
野性不改 2021-01-05 12:10

Here is my query in Codeigniter

$this->db->select(\'p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value\',         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-05 12:27

    The CodeIgniter documentation indicates case statements within the order by clause isn't supported within the Active Record class. I'd recommend refactoring the SQL call so that the case statement is part of the select clause. Something like the following should do the trick.

    $this->db->select("p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name, al.length_value, CASE p.submit_to WHEN 'writer' THEN 2 WHEN 'students' THEN 1 ELSE 0 END AS ordered_submit_to",FALSE);
    $this->db->from($this->_tbl_projects . ' as p');
    $this->db->join($this->_tbl_client_details . ' as c', 'c.id = p.client_id', 'left');
    $this->db->join($this->_tbl_users . ' as u', 'u.id = c.user_id', 'left');
    $this->db->join($this->_tbl_project_types . ' as pt', 'pt.project_type_id = p.project_type_id', 'left');
    $this->db->join($this->_tbl_specializations . ' as s', 's.specialization_id = p.specialization_id', 'left');
    $this->db->join($this->_tbl_article_length . ' as al', 'al.article_length_id = p.article_length_id', 'left');
    $this->db->order_by('ordered_submit_to', 'ASC');
    $this->db->order_by('p.request_end_date', 'ASC');
    

提交回复
热议问题