• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
can i use SQL UNION Syntax with CI query builder

#1
someone help me how can i use SQL UNION Syntax with CI query builder.... Confused
Reply

#2
UNION is not supported directly by the query builder as far as I know. On CI3 I apply something like this:

Code:
$tables = array();

        $tables[] = 'table1';
        $tables[] = 'table2';
        $tables[] = 'table3';

        $subqueries = array();

        foreach ($tables as $t) {

            // Change these queries accordingly:
            $this->db
                ->select('category_id')
                ->from($t)
                ->where('sp_id', $sp_id);

            $subqueries[] = '('.$this->db->get_compiled_select().')';
        }

        $sql = implode(' UNION ', $subqueries);
        // or if necessary:
        //$sql = implode(' UNION DISTINCT ', $subqueries);

        $result = $this->db->query($sql)->result_array();
Reply

#3
Thanks. Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.