activerecord – Codeigniter passes the output of one selection to the IN clause of another selection

I want to take advantage of the security of Active Records in Codeigniter. I have the following code in CI:

$ this-> db-> select (& # 39; sno & # 39;);
$ this-> db-> from (& # 39; table1 & # 39;);
$ this-> db-> where ("cid", $ cid);
$ subquery = $ this-> db-> get_compiled_select ();

$ this-> db-> select ("pid") -> from ("table2");
$ this-> db-> group_start ();
$ this-> db-> group_start ();
$ this-> db-> where ("col1 <",$cin);
        $this->db-> where ("col2>", $ cin);
$ this-> db-> group_end ();
$ this-> db-> or_group_start ();
$ this-> db-> where ("col1 <",$cout);
        $this->db-> where ("col2>", $ cout);
$ this-> db-> group_end ();
$ this-> db-> or_group_start ();
$ this-> db-> where ("col1>", $ cin);
$ this-> db-> where ("col1 <",$cout);
    $this->db-> group_end ();
$ this-> db-> or_group_start ();
$ this-> db-> where ("col1", $ cin);
$ this-> db-> or_where ("col2", $ cout);
$ this-> db-> or_where ("col1", $ cout);
$ this-> db-> or_where ("col2", $ cin);
$ this-> db-> group_end ();
$ this-> db-> or_group_start ();
$ this-> db-> where ("col2>", $ cin);
$ this-> db-> where ("col2 <",$cout);
    $this->db-> group_end ();
$ this-> db-> group_end ();
$ this-> db-> where_in ("col3", $ subquery);
$ sbquery = $ this-> db-> get_compiled_select ();

$ this-> db-> from (& # 39; table1 & # 39;);
$ this-> db-> where ("tid", $ cid);
$ this-> db-> where_not_in ("cid", $ sbquery);
$ result = $ this-> db-> get_compiled_select ();
var_dump ($ result);
exit;

This code shows the following query (as a result of var_dump):

SELECT * FROM `table1` WHERE` tid` = & # 39; 6 & # 39; And `cid` NOT IN (& # 39; SELECT` pid`  nFROM` table2`  nWHERE ( n ( n`col1` < '2018-12-10'nAND `col2` >  & # 39; 2018-12-10  & # 39;  n)  nOR ( n`col1` < '2018-12-16'nAND `col2` >  & # 39; 2018-12-16  & # 39;  n)  nOR ( n`col1`>  & # 39; 2018-12-10  & # 39;  nAND `col1` < '2018-12-16'n )nOR (n`col1` = '2018-12-10'nOR `col2` = '2018-12-16'nOR `col1` = '2018-12-16'nOR `col2` = '2018-12-10'n )nOR (n`col2` >  & # 39; 2018-12-10  & # 39;  nAND `col2` < & # 39; 2018-12-16  & # 39;  n)  n)  nAND `col3` IN ( & # 39; SELECT` sno` \ nFROM` table1`   NDONDE `cid` = \ & # 39; 6 \ & # 39;  & # 39;) & # 39;)

This gives incorrect results when I use the following code (the last 3 lines of code):

$ result = $ this-> db-> get () -> result ();
var_dump ($ result);
exit;

If I hit the same query directly in SQL, I get the same incorrect results.

But, if I delete bars, single quotes, new line and other special characters, to perform the query rewritten directly in SQL, I get perfectly accurate results.

SELECT * FROM `table1` WHERE` tid` = & # 39; 6 & # 39; And `cid` NOT IN (SELECT` pid` FROM` table2` WHERE ((`col1` < '2018-12-10' AND `col2` > & # 39; 2018-12-10 & # 39;) OR (`col1` < '2018-12-16' AND `col2` > & # 39; 2018-12-16 & # 39;) OR (`col1`> & # 39; 2018-12-10 & # 39; Y` col1` < '2018-12-16') OR (`col1` = '2018-12-10' OR `col2` = '2018-12-16' OR `col1` = '2018-12-16' OR `col2` = '2018-12-10') OR (`col2` > & # 39; 2018-12-10 & # 39; And `col2` <& # 39; 2018-12-16 & # 39;)) Y` col3` IN (SELECT` sno` FROM` table1` WHERE `cid` = & # 39; 6 & # 39;))

Please suggest a solution.