dqw70970 2017-02-27 16:03
浏览 460
已采纳

使用CodeIgniter get_where来链接'和'以及'或'语句

I am trying to access data in a local database (on my vm), and I have to use CodeIgniter's query building classes to get the data. I have this query which I have figured out in sql:

select message, created
from logs
where username = 'user'
    and (
        created > '1487695796'
        and created < '1487782196'
        )
    and (
        message = 'login failure'
        or message = 'login success'
        or message = 'log out'
        )
order by created asc

My biggest question is how can I chain the 'and's and 'or's in a get_where statement in CodeIgniter? I have looked and saw I could put things in an array for the 'WHERE' portion, but I haven't seen how I can place things in for the 'or's (since everything in the array is an 'and'. I MUST use get_where (can't do 'get->where'), so if there is a way to do it, please let me know!

Thank you for taking the time to read my question!

  • 写回答

3条回答 默认 最新

  • dropbox1111 2017-02-27 16:15
    关注

    Since Codeigniter 3.0 the query builder class supports Query grouping

    from the docs:

    $this->db->select('*')->from('my_table')
            ->group_start()
                    ->where('a', 'a')
                    ->or_group_start()
                            ->where('b', 'b')
                            ->where('c', 'c')
                    ->group_end()
            ->group_end()
            ->where('d', 'd')
    ->get();
    
    // Generates:
    // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'
    

    Edit: in your example you would use:

    $this->db->select('message, created')->from('logs')
            ->where('username', 'user')
            ->group_start()
                    ->where('created >', '1487695796')
                    ->where('created <', '1487782196')
            ->group_end()
            ->group_start()
                    ->where('message', 'login failure')
                    ->or_where('message', 'login success')
                    ->or_where('message', 'log out')
            ->group_end()               
            ->order_by('created', 'ASC')  
    ->get(); 
    

    or

    $this->db->select('message, created')
            ->group_start()
                    ->where('created >', '1487695796')
                    ->where('created <', '1487782196')
            ->group_end()
            ->group_start()
                    ->where('message', 'login failure')
                    ->or_where('message', 'login success')
                    ->or_where('message', 'log out')
            ->group_end()               
            ->order_by('created', 'ASC')  
    ->get_where('logs', array('username' => 'user') );
    
    // both generate:
    //SELECT `message`, `created` 
    //FROM `logs` 
    //WHERE `username` = 'user' 
    //AND ( `created` > '1487695796' AND `created` < '1487782196' ) 
    //AND ( `message` = 'login failure' OR `message` = 'login success' OR `message` = 'log out' ) 
    //ORDER BY `created` ASC
    

    to check if the Codeigniter generated query matches your SQL, you can use:

    echo $this->db->last_query(); // echos last query string
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。