简体   繁体   English

Codeigniter活动记录And或where子句中具有多个和嵌套组的组合

[英]Codeigniter Active record And Or combinations in where clause with multiple & nested groups

I have a complex SQL query and I want to implement it through Active Records. 我有一个复杂的SQL查询,我想通过Active Records实现它。 This query has several AND / OR clauses grouped together with another criteria. 此查询具有与另一个条件组合在一起的多个AND / OR子句。 I have gone through various articles where they said that we can use group_start() and group_end() but I am wondering if a group can be started inside another group too? 我浏览了多篇文章,他们说我们可以使用group_start()group_end(),但是我想知道是否也可以在另一个组中启动一个组吗? The resulting serial numbers needs to be excluded from the result set to be produced by the outer query. 需要将结果序列号从外部查询产生的结果集中排除。 Actually I tried using Join here, but it didn't work. 实际上,我在这里尝试使用Join,但是没有用。 Any working idea regarding joins here will be appreciable too. 关于连接的任何可行的想法也将是可理解的。

As you can see in the query below, I have used double round brackets to represent multiple groups inside a group. 正如您在下面的查询中看到的那样,我使用了双圆括号来表示一个组中的多个组。 The resulting serial numbers needs to be excluded from the results of outer query too. 生成的序列号也需要从外部查询的结果中排除。 Please tell me what will be its Codeigniter Active Record equivalent code. 请告诉我它的Codeigniter Active Record等效代码是什么。

select * from table2 WHERE NOT table2.serial IN (select columnname from table where ((col < val and val < col) or (col < val and val < col) or(val=col and val=col)) AND incol=intval AND intcol=intval)

Here, col is the column name, val is a value of DATE type, intval is an Integer value 在这里,col是列名,val是DATE类型的值,intval是一个整数值

Try this syntax 试试这个语法

$this->db->select("*")->from("table");
    $this->db->group_start();
        $this->db->group_start();
            $this->db->where("val <",'col');
            $this->db->where("val <",'col');
        $this->db->group_end();
        $this->db->or_group_start();
            $this->db->or_where("val <",'col');
            $this->db->where("val <",'col');
        $this->db->group_end();
        $this->db->or_group_start();
            $this->db->or_where("val ",'col');
            $this->db->where("val ",'col');
        $this->db->group_end();
    $this->db->group_end();
    $this->db->where("incol ",'intval');
    $this->db->where("incol ",'intval');
    $this->db->get();
    $last_query = $this->db->last_query();

    $this->db->select('*')->from('table2');
    $this->db->where_not_in('serial',$last_query);
    $this->db->get();
    echo $this->db->last_query();

The query string produced by the above is 上面产生的查询字符串是

SELECT * FROM `table2` WHERE
     `serial` NOT IN(
    SELECT columnname FROM `table` WHERE
    (
        (`val` < 'col' AND `val` < 'col') OR
        (`val` < 'col' AND `val` < 'col') OR
        (`val` = 'col' AND `val` = 'col') 
    ) AND `incol` = 'intval' AND `incol` = 'intval'
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM