我想从“table1”中删除 (user_id = 5) 的那些行,但我应该检查这些帖子是否“(table2 中的 title = title1)。我使用 Codeigniter,在尝试删除时收到此错误:“除非包含“where”或“like”子句,否则不允许删除。”您能帮我检查一下下面的代码有什么问题吗?
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;
}
}
使用子查询。
example
#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');
参考
- 从这里被盗:https://stackoverflow.com/a/16303021/1275832 https://stackoverflow.com/a/16303021/1275832
- 关于子查询:http://www.mysqltutorial.org/mysql-subquery/ http://www.mysqltutorial.org/mysql-subquery/
- 编译选择:https://github.com/NTICompass/CodeIgniter-Subqueries https://github.com/NTICompass/CodeIgniter-Subqueries
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)