Sql 如何与Codeigniter中的ON CASE连接

Sql 如何与Codeigniter中的ON CASE连接,sql,codeigniter,Sql,Codeigniter,我想在连接条件下使用用例,但它不再工作了 $this->db->join('user_profile ON sender.user_id = CASE WHEN mailbox.sender_user_id = 1 THEN mailbox_recipient.recipient_user_id ELSE mailbox.sender_user_id END');

我想在连接条件下使用用例,但它不再工作了

$this->db->join('user_profile ON sender.user_id = CASE WHEN mailbox.sender_user_id = 1 
                             THEN mailbox_recipient.recipient_user_id
                             ELSE mailbox.sender_user_id END');
显示这样的错误

JOIN `user_profile ON sender`.`user_id = CASE WHEN mailbox`.`sender_user_id = 1 THEN mailbox_recipient`.`recipient_user_id ELSE mailbox`.`sender_user_id` `END`

单引号``disorganized

在join语句之前添加此语句

$this->db->protect_idenifiers(FALSE);
它会去除虱子。在查询的get()语句之后,添加以下内容

$this->db->protect_idenifiers(TRUE);

为了避免安全问题。

我尝试使用其他替代方案

$this->db->join('user_profile AS sender', 'sender.user_id = '.$this->table_name.'.sender_user_id');
$this->db->join('user_profile AS recipient', 'recipient.user_id = '.$this->table_name.'_recipient.recipient_user_id');
然后在我的列表中选择如下:

$this->db->select(
      $this->table_name.'.*, 
      (CASE WHEN mailbox.sender_user_id = '.$this->user_id.' THEN recipient.fullname ELSE sender.fullname END) AS user_name');
只需在case语句中添加()

$this->db->join('user_profile ON sender.user_id = (CASE WHEN mailbox.sender_user_id = 1 THEN mailbox_recipient.recipient_user_id
ELSE mailbox.sender_user_id END)');

结束后添加
大小写之前和
您的语法错误$这->数据库->加入('comments','comments.id=blogs.id')@438sunil:是的,我的错。但是,如果在Codeigniter查询生成器中使用带有CASE的连接,它仍然不起作用。除了使用$this->db->query();