Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>by modifying somnath huluks answer, i add these following variable and functions to DB_Active_rec class as follows:</p> <pre><code>class DB_Active_records extends CI_DB_Driver { .... var $unions; .... public function union_push($table = '') { if ($table != '') { $this-&gt;_track_aliases($table); $this-&gt;from($table); } $sql = $this-&gt;_compile_select(); array_push($this-&gt;unions, $sql); $this-&gt;_reset_select(); } public function union_flush() { $this-&gt;unions = array(); } public function union() { $sql = '('.implode(') union (', $this-&gt;unions).')'; $result = $this-&gt;query($sql); $this-&gt;union_flush(); return $result; } public function union_all() { $sql = '('.implode(') union all (', $this-&gt;unions).')'; $result = $this-&gt;query($sql); $this-&gt;union_flush(); return $result; } } </code></pre> <p>therefore you can virtually use unions without dependencies to db_driver.</p> <p>to use union with this method, you simply make regular active record queries, but calling union_push instead of get.</p> <p>note: you have to ensure your queries have matching columns like regular unions</p> <p>example:</p> <pre><code> $this-&gt;db-&gt;select('l.tpid, l.lesson, l.lesson_type, l.content, l.file'); $this-&gt;db-&gt;where(array('l.requirement' =&gt; 0)); $this-&gt;db-&gt;union_push('lessons l'); $this-&gt;db-&gt;select('l.tpid, l.lesson, l.lesson_type, l.content, l.file'); $this-&gt;db-&gt;from('lessons l'); $this-&gt;db-&gt;join('scores s', 'l.requirement = s.lid'); $this-&gt;db-&gt;union_push(); $query = $this-&gt;db-&gt;union_all(); return $query-&gt;result_array(); </code></pre> <p>would produce:</p> <pre><code>(SELECT `l`.`tpid`, `l`.`lesson`, `l`.`lesson_type`, `l`.`content`, `l`.`file` FROM `lessons` l WHERE `l`.`requirement`=0) union all (SELECT `l`.`tpid`, `l`.`lesson`, `l`.`lesson_type`, `l`.`content`, `l`.`file` FROM `lessons` l JOIN `scores` s ON `l`.`requirement`=`s`.`lid`) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload