Codeigniter无法使用join方法从数据库表中删除行 [英] Codeigniter cannot delete rows from database table using join method
本文介绍了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');
参考
- 从此处被盗: http://stackoverflow.com/a/16303021/1275832
- 关于子查询: http://www.mysqltutorial.org/mysql-subquery/
- 编译选择: https://github.com/NTICompass/ CodeIgniter-Subqueries
- stolen from here: http://stackoverflow.com/a/16303021/1275832
- about subqueries: http://www.mysqltutorial.org/mysql-subquery/
- compiled select: https://github.com/NTICompass/CodeIgniter-Subqueries
References
这篇关于Codeigniter无法使用join方法从数据库表中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文