分组凡在codeigniter条款条款、codeigniter

2023-09-08 15:42:45 作者:弃疗

我想用的活动记录在codeigniter产生下面的SQL code:

I want to produce the following SQL code using Active Records in Codeigniter:

WHERE name != 'Joe' AND (age < 69 OR id > 50)

执行以下操作似乎只要我能,我无法弄清楚如何将它们分组

Doing the following seems to be as far as I can get, I cant figure out how to group them

$this->db->select()->from('users')->where('name !=', 'Joe')->where('age <', 69)->or_where('id <', $id); 

任何想法?我的SQL查询是太复杂,所以我不希望重写一切传统的SQL。

Any ideas? My SQL query is too complex so I dont wish to rewrite everything in traditional SQL.

更新

我的SQL code动态的取决于进入该模型方法的某些参数的值产生。与不能够使用括号中的问题会导致一个问题,因为运营商precedence是这样的:之前首先计算或

My SQL code is dynamically generated depending on the values of certain parameters passed into the model method. The problem with not being able to use parenthesis causes a problem because the operator precedence is such that AND is evaluated first before OR.

*这里是我的活动记录code,其中也有一些其他的code之前和之后它一大块:

*Here is a chunk of my active records code, where there are some other code before and after it:

            ... some $this->db->where() ...
            ... some $this->db->where() ...

    if($price_range) {
        $price_array = explode('.', $price_range);
        for($i = 0; $i < count($price_array); $i++) {
            if($i == 0) {
                $this->db->where('places.price_range', $price_array[$i]);
            } else {
                $this->db->or_where('places.price_range', $price_array[$i]);
            }
        }

    }

            ... some $this->db->where() ...
            ... some $this->db->where() ...

这个问题是因为我使用 $这个 - &GT; DB-&GT; or_where()它引入了一个条款,引发运营商precedence陷入混乱,而不能使用()来改变顺序。

The problem comes because I am using $this->db->or_where() which introduces a OR clause that throws the operator precedence into disarray without being able to use ( ) to change the order.

**有什么办法解决呢? **

** Is there any way to solve this? **

推荐答案

解决。动态生成SQL查询,并将其插入 $这个 - &GT; DB-化合物其中()。谢谢你们!

Solved. Dynamically generate the SQL query and plug it into $this->db->where(). Thanks guys!