使用CI activerecords组合`where`和`like`语句 [英] Combining `where` and `like` statements by using the CI activerecords

查看:131
本文介绍了使用CI activerecords组合`where`和`like`语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

剪短的故事:是可能的,如果是 - 我如何构建看起来有点像这一个的查询

To cut a long story short: Is it possible and if it is - how can I build a query that looks somewhat like this one

SELECT * FROM a
    WHERE row = 1 AND 
    (other_row LIKE '%..%' OR another_row LIKE '%..%')

基本上我无法找到解决这个问题的方法。我只是似乎无法计算如何添加括号到activerecords查询。这是可能吗?

Basically I cann't come up / find a solution to this problem. I just cann't seem to figure how to add the brackets to the activerecords query. Is that even possible?

我当前的代码:

$data = $this->where('row', 1)
    ->like('another_row', '...')        
    ->or_where('row', 1)
    ->like('other_row', $search)                
    ->get('a')
    ->result();

结果:

SELECT * FROM (`a`) WHERE `row` = 1 OR `row` = 1 AND `another_row` LIKE '%...%' AND other_row` LIKE '%...%'


推荐答案

   $query = $this->db->select('*')
            ->from('a')
            ->where('row',1)
            ->where("(other_row LIKE '%%' OR another_row LIKE '%%' )")
            ->get();


    foreach ($query->result() as $row) {
        //do sth.
    }

您可以编写自定义查询字符串(来自活动记录类)

You can write custom query string (from active record class)

 Custom string:
 You can write your own clauses manually:

 $where = "name='Joe' AND status='boss' OR status='active'";

 $this->db->where($where);

这篇关于使用CI activerecords组合`where`和`like`语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆