Codeigniter无法使用join方法从数据库表中删除行 [英] Codeigniter cannot delete rows from database table using join method

查看:211
本文介绍了Codeigniter无法使用join方法从数据库表中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从'table1'删除(user_id = 5),但我应该检查这些帖子'(title = title1在table2)。我使用Codeigniter和我得到这个错误,同时尝试删除:'删除是不允许的,除非它们包含一个where或like子句。你能请我帮助我检查下面的代码是什么错误。

I want to delete from ‘table1’ those rows where (user_id = 5) but I should check if those posts’ (title = title1 in table2). I use Codeigniter and I get this error while trying to delete: ‘Deletes are not allowed unless they contain a "where" or "like" clause.’ Could you please help me to check what is wrong with my code below.

table1:

table2:

public function delete($title, $user_id){ 

    $this->db->select('table1.*');
    $this->db->from('table1','table2');   
    $this->db->where('table1.user_id', $user_id); 
    $this->db->where('table2.title', $title);
    $this->db->join('table2','table1.post_id=table2.post_id');

     $query = $this->db->get();   

        if ($query && $query->num_rows() > 0) {

    $this->db->delete('table1.*');
    $this->db->from('table1','table2');   
    $this->db->where('table1.user_id', $user_id); 
    $this->db->where('table2.title', $title);
    $this->db->join('table2','table1.post_id=table2.post_id');
    return true;
            } 
    else {
    return false;
    }

   } 


推荐答案

使用子查询。

#Create where clause
$this->db->select('id');
$this->db->from('table2');
$this->db->where('table2.title', $title);
$where_clause = $this->db->get_compiled_select();

#Create main query
$this->db->where('table1.user_id', $user_id); 
$this->db->where("`id` NOT IN ($where_clause)", NULL, FALSE);
$this->db->delete('table1'); 



参考



查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆