2014-07-31 15 views
5

Đây là câu hỏi của tôi trong CodeIgniterCodeIgniter: Lỗi trong ORDER BY truy vấn TRƯỜNG HỢP

$this->db->select('p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value',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("CASE p.submit_to 
            WHEN '' THEN 0 
            WHEN 'writer' THEN 1 
            ELSE 2 
           END, p.request_end_date asc",FALSE); 

Nó In

SELECT p.*, u.firstname, u.lastname, s.title AS industry, pt.type_name, al.length_value 
FROM (`projects` as p) 
LEFT JOIN `client_details` as c ON `c`.`id` = `p`.`client_id` 
LEFT JOIN `users` as u ON `u`.`id` = `c`.`user_id` 
LEFT JOIN `project_types` as pt ON `pt`.`project_type_id` = `p`.`project_type_id` 
LEFT JOIN `specializations` as s ON `s`.`specialization_id` = `p`.`specialization_id` 
LEFT JOIN `article_length` as al ON `al`.`article_length_id` = `p`.`article_length_id` WHERE `p`.`client_id` = '26' AND `p`.`status` IN (2, 3) 
ORDER BY 
     `CASE` p.submit_to 
      WHEN '' THEN 0 
      WHEN 'writer' THEN 1 
      ELSE 2 
     END, `p`.`request_end_date` asc 

đây trong truy vấn in, TRƯỜNG HỢP được in bởi 'TRƯỜNG HỢP `, vì vậy sql ném lỗi.

Tôi có thể giải quyết nó bằng cách nào?

Cấu trúc của lĩnh vực submit_to là

submit_to enum('','writer','students') NOT NULL 

Trả lời

8

Các tài liệu CodeIgniter chỉ ra trường hợp báo cáo trong vòng trật tự quy định tại khoản không được hỗ trợ trong Ghi lớp Active Directory. Tôi muốn khuyên bạn nên tái cấu trúc cuộc gọi SQL sao cho câu lệnh case là một phần của mệnh đề select. Một cái gì đó như sau nên làm các 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'); 
+0

Cảm ơn bạn đã trả lời. Cập nhật của bạn hoạt động nhưng không cho kết quả theo thứ tự đúng. Tôi muốn dữ liệu để submit_to = '' sau đó submit_to = 'sinh viên' và cuối cùng submit_to = 'nhà văn'. Nội dung cập nhật của bạn cho kết quả theo thứ tự này 1. '' 2. 'writer' 3. 'students'. – Napster

+0

Đã cập nhật để khớp với đơn đặt hàng đã được sắp xếp lại của bạn. – Thomas

0

I figured it out thế nào để tránh thoát TRƯỜNG HỢP từ.

Bạn có thể sửa đổi $ _reserved_identifiers biến bên trong CI_DB_driver lớp. Nếu bạn không muốn sửa đổi toàn bộ tệp, bạn có thể chỉ cần thay đổi nó bên trong lớp Mô hình của bạn ngay trước truy vấn đó.

$this->db->_reserved_identifiers = array('*','CASE'); 

$this->db->select('p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value',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("CASE p.submit_to 
           WHEN '' THEN 0 
           WHEN 'writer' THEN 1 
           ELSE 2 
          END, p.request_end_date asc",FALSE); 

(Tôi không biết nếu nó làm việc trên nhánh 3.x. Đó là làm việc tốt trên nhánh 2.x)

0

Tôi tìm thấy một giải pháp tốt trong another answer on SO như vậy trong trường hợp bạn đã đăng ký trang này và chưa tìm thấy trang đó, tôi sẽ đăng lại ở đây.


Bạn có thể sử dụng một tuyên bố trường hợp nếu bạn quấn báo cáo trường hợp trong ngoặc.

$this->db->order_by(" 
    (CASE p.submit_to 
     WHEN '' THEN 0 
     WHEN 'writer' THEN 1 
     ELSE 2 
    END), 
    p.request_end_date asc" 
); 
0

Tôi đã thử nghiệm hôm nay và hoạt động.

Bạn có thể sử dụng nó như bạn đã nói bất cứ khi nào bạn sử dụng thông số thứ hai FALSE để tắt thoát.

$this->db->order_by("CASE p.submit_to 
            WHEN '' THEN 0 
            WHEN 'writer' THEN 1 
            ELSE 2 
           END, p.request_end_date asc",FALSE); 

Chúc mừng.

Các vấn đề liên quan