Welcome Guest, Not a member yet? Register   Sign In
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




Theme © iAndrew 2016 - Forum software by © MyBB