Welcome Guest, Not a member yet? Register   Sign In
can i use SQL UNION Syntax with CI query builder
#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


Messages In This Thread
RE: can i use SQL UNION Syntax with CI query builder - by ivantcholakov - 09-12-2015, 07:46 AM



Theme © iAndrew 2016 - Forum software by © MyBB