or_where里面有函数活动记录

时间:2015-03-13 10:44:07

标签: php mysql codeigniter

我想问一下你是否有人知道如何将一个or_where放在codeigniter的哪个位置?

我想创建一个查询,如下所示

select *
from table
where
(
  (in = 0 and call_in = 1) or
  (out = 0 and call_out = 1)
) and
mid = 0
ORDER BY id DESC
limit 1

我创造了类似这样的东西

$result = $mysql_handler->select('*')
                        ->from("table")
                        ->where(
                               array(
                                 "in"       => 0,
                                 "call_in"  => 1
                               )
                          )
                        ->or_where(
                              array(
                                 "out"      => 0,
                                 "call_out" => 1
                              )
                         )
                        ->where("mid", 0)
                        ->order_by("id", "DESC")
                        ->limit(1)
                        ->get();

但我知道这不对,因为这段代码会生成类似的东西

select *
from table
where
   (in = 0 and call_in = 1) or
   (out = 0 and call_out = 1) and
   mid = 0
ORDER BY id DESC
limit 1

我考虑将or_where放在where子句中,但我不确定这是否正确或如何操作。请建议谢谢。

1 个答案:

答案 0 :(得分:2)

您无法根据需要合并whereor_whereor_where只能在需要OR个查询的地方使用。您可以尝试这个解决方案

    $this->db->from('table');
    $this->db->select('*');
    $this->db->where('((in = 0 and call_in = 1) OR (out = 0 and call_out = 1))');
    $this->db->where("mid", 0);
    $this->db->order_by("id", "DESC");
    $this->db->limit(1);
    $result=$this->db->get();

或者

$result = $mysql_handler->select('*')
        ->from("table")
        ->where('((in = 0 and call_in = 1) OR (out = 0 and call_out = 1))')            
        ->where("mid", 0)
        ->order_by("id", "DESC")
        ->limit(1)
        ->get();