以下是 Codeigniter
中的查询$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);
打印
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
在打印查询中, CASE 由`CASE` 打印,因此sql会抛出错误。
我该如何解决?
submit_to字段的结构是
submit_to enum('','writer','students') NOT NULL
答案 0 :(得分:8)
CodeIgniter文档表明Active Record类不支持order by子句中的case语句。我建议重构SQL调用,以便case语句是select子句的一部分。像下面这样的东西可以解决问题。
$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');
答案 1 :(得分:2)
我在another answer on SO找到了一个很好的解决方案,所以如果你已经登陆这个页面并且没有找到那个,我会在这里重新发布。
如果将case语句包装在括号中,则可以使用case语句。
$this->db->order_by("
(CASE p.submit_to
WHEN '' THEN 0
WHEN 'writer' THEN 1
ELSE 2
END),
p.request_end_date asc"
);
答案 2 :(得分:0)
我想出了如何避免转义 CASE 字。
您可以在 CI_DB_driver 类中修改 $ _ reserved_identifiers 变量。如果您不想修改整个文件,只需在该查询之前在Model类中更改它。
$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);
(我不知道它是否在3.x分支上工作。它在2.x分支上工作得很好)
答案 3 :(得分:0)
我今天测试了它并且它正在工作。
只要你使用第二个参数FALSE来禁用转义,就可以像使用它一样使用它。
$this->db->order_by("CASE p.submit_to
WHEN '' THEN 0
WHEN 'writer' THEN 1
ELSE 2
END, p.request_end_date asc",FALSE);
问候。
答案 4 :(得分:0)
在“ SELECT”(而不是“ ORDER_BY”)中设置“ CASE WHEN”:
$this->db->select('p.*,u.firstname, u.lastname,s.title AS industry, pt.type_name , al.length_value',FALSE);
$this->db->select("CASE p.submit_to WHEN '' THEN 0 WHEN 'writer' THEN 1 ELSE 2 END order_column_number",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("order_column_number ASC, p.request_end_date ASC");