在CodeIgniter Active Record的删除方法中使用OR

时间:2012-11-30 21:36:50

标签: codeigniter activerecord

我需要能够将以下内容转换为Ci的活动记录删除方法,但我不知道如何在delete语句中使用OR。你能告诉我怎么做到这一点吗?

$this->db->query("DELETE FROM friend WHERE userid_friends = '{$userid}' AND friendId_friends = '{$targetedUserId}' OR userid_friends = '{$targetedUserId}' AND friendId_friends = '{$userid}' ");

1 个答案:

答案 0 :(得分:2)

我猜你在尝试这个:

$this->db->query("DELETE FROM friend WHERE 
   ( userid_friends = '{$userid}' AND friendId_friends = '{$targetedUserId}')  OR 
   ( userid_friends = '{$targetedUserId}' AND friendId_friends = '{$userid}')  ");

(注意两个AND子句的附加括号)

但实际上你不使用CI的“DELETE”方法只是一个查询。

使用活动记录删除类似于:

$this->db->where("userid_friends = '{$userid}' AND friendId_friends = '{$targetedUserId}'");
$this->db->or_where("userid_friends = '{$targetedUserId}' AND friendId_friends = '{$userid}'");
$this->db->delete('friend');

对于复杂查询的调试,我建议您使用

echo $this->db->last_query();

因为它向您显示了Active记录方法如何呈现最终查询。