【问题标题】:or_where inside where function active recordsor_where 里面 where 函数活动记录
【发布时间】:2015-05-15 19:34:59
【问题描述】:

我想问一下你们中是否有人知道如何在 codeigniter 中的 where 中放置 or_where?

我想创建如下所示的查询

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 子句中,但我不确定这是否正确或如何做。请指教谢谢。

【问题讨论】:

    标签: php mysql codeigniter


    【解决方案1】:

    您不能随意组合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();
    

    【讨论】:

    • 在 where('((in = 0 and call_in = 1) OR (out = 0 and call_out = 1))') 里面有没有办法让它像一个准备好的陈述?比如 where('((in = ? and call_in = ?) OR (out = ? and call_out = ?))', array(0,1,0,1)) 我不确定这是否可能
    猜你喜欢
    • 2012-04-09
    • 2023-03-08
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    相关资源
    最近更新 更多