2

I've two categories and I want to fetch three records of each category later I found this link UNION query with codeigniter's active record pattern after this I change my DB_Active_rec file and add this code also

var $unions = array();

public function union_push($table = '') {
    if ($table != '') {
        $this->_track_aliases($table);
        $this->from($table);
    }

    $sql = $this->_compile_select();

    array_push($this->unions, $sql);
    $this->_reset_select();
}

public function union_flush() {
    $this->unions = array();
}

public function union() {
    $sql = '(' . implode(') union (', $this->unions) . ')';
    $result = $this->query($sql);
    $this->union_flush();
    return $result;
}

public function union_all() {
    $sql = '(' . implode(') union all (', $this->unions) . ')';
    $result = $this->query($sql);
    $this->union_flush();
    return $result;
}

and then I create codeigniter's function based query like this

$this->db->select("*");
$this->db->from("media m");
$this->db->join("category c", "m.category_id=c.id", "INNER");
$this->db->order_by("m.media_files", "DESC");
$this->db->limit(3);
$this->db->union_push();
$this->db->select("*");
$this->db->from("media m");
$this->db->join("category c", "m.category_id=c.id", "INNER");
$this->db->order_by("m.media_files", "DESC");
$this->db->limit(3);
$this->db->union_push();
$getMedia = $this->db->union_all();

create this

(SELECT * FROM media m INNER JOIN category c ON 
m.category_id = c.id ORDER BY m.media_files DESC LIMIT 3)
UNION ALL
(SELECT * FROM media m INNER JOIN category c ON 
m.category_id = c.id ORDER BY m.media_files DESC LIMIT 3)

Now it is fetching records but not properly I want to use only query, it showing six records first query fetch 3 records and second query fetch three records now records are duplicate I check the id of records it is 6,5,4 and again 6,5,4. It can be done also by PHP but I want to use query. Thanks in advance

7
  • Actually I'm using codeigniter's built in functions like $this->db->select('*'); and others functions so how do I achieve it. Sep 3, 2014 at 14:23
  • Is there any way to achieve it? Sep 4, 2014 at 10:12
  • Mr GolezTrol now see my question after editing. Sep 4, 2014 at 11:27
  • I see you made great effort to improve it. I voted to reopen it, and I removed the downvote and my now obsolete comments. I don't know much about Codeigniter though and I currently don't have time to dive into it, so I hope someone else will have an answer for you.
    – GolezTrol
    Sep 4, 2014 at 11:29
  • I post simple query which is creating in codeigniter Sep 4, 2014 at 12:11

1 Answer 1

0

I dont know code-igniter, but basicly you want it to do the union first and then apply the order by over the whole set. This would require a subquery. It should result in the following SQL query:

select * from
    ((SELECT * FROM media m INNER JOIN category c ON m.category_id = c.id )
    UNION ALL
    (SELECT * FROM media m INNER JOIN category c ON m.category_id = c.id)) T
ORDER BY m.media_files DESC LIMIT 3

Hope it helps you some.

3
  • it is giving me error Error Code: 1060 Duplicate column name 'id' Sep 4, 2014 at 14:34
  • replace the 2 select * with select [fieldnames you want]
    – Alfons
    Sep 8, 2014 at 7:43
  • Error Code: 1222 The used SELECT statements have a different number of columns after this query SELECT * FROM ((SELECT * FROM kt_media m INNER JOIN kt_category c ON m.category_id = c.id ) UNION ALL (SELECT m.media_files FROM kt_media m INNER JOIN kt_category c ON m.category_id = c.id)) T ORDER BY m.media_files DESC LIMIT 3 Sep 8, 2014 at 9:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.