我已经检查过有关此问题的其他问题,但无法解决问题
$this->db->where(array('sender_deletedS' => 1, 'receiver_deleted' => 0 ));
$this->db->or_where(array('sender_deletedS' => 0, 'receiver_deleted' => 1 ));
$this->db->or_where(array('sender_deletedS' => 0, 'receiver_deleted' => 0 ));
$query2 = $this->db->get('message');
它产生
SELECT * FROM `message` WHERE `sender_deletedS` = 1 AND `receiver_deleted` =0 OR `sender_deletedS` =0 OR `receiver_deleted` = 1 OR `sender_deletedS` =0 OR `receiver_deleted` =0
但我想要这个
SELECT * FROM `message` WHERE `sender_deletedS` = 1 AND `receiver_deleted` =0 OR `sender_deletedS` =0 AND `receiver_deleted` = 1 OR `sender_deletedS` =0 AND `receiver_deleted` =0
答案 0 :(得分:1)
$sql = "sender_deleted = 1 AND receiver_deleted = 0 OR sender_deleted = 0 AND receiver_deleted = 1 OR
sender_deleted = 0 AND receiver_deleted = 0
";
$this->db->where($sql);
$this->db->get('message') ;
可以使用变量存储和执行语句,如上例中所示
答案 1 :(得分:1)
如果您使用查询生成器或活动记录如此混乱,请尝试传统的SQL方法。它也很容易理解
$this->db->query("SELECT * FROM `message` WHERE `sender_deletedS` = 1 AND `receiver_deleted` =0 OR `sender_deletedS` =0 AND `receiver_deleted` = 1 OR `sender_deletedS` =0 AND `receiver_deleted` =0");
or_where
将生成一个OR
已加入的声明。如果它合并为AND
。
示例01
$this->db->or_where('user_id =', 0);
$this->db->or_where('member_id =', 1);
输出
WHERE user_id = 0 OR member_id = 1
示例02
$this->db->or_where(array('user_id' => 0, 'member_id' => 1 ));
输出
WHERE user_id = 0 AND member_id = 1
示例03
$this->db->or_where(array('user_id' => 0, 'member_id' => 1 ));
$this->db->or_where('library_id =', 0);
输出
WHERE user_id = 0 AND member_id = 1 OR library_id = 0