I have query that counts ids in joined table. I need to do an union of returned table with another table in database.
The table returned from first query has 4 columns:
- group_id
 - count
 - name
 - description
 
The table I want to unite with the first one has 3 columns:
- group_id
 - name
 - description
 
I run query through phpmyadmin and it worked perfectly. Query:
SELECT group_id, size, name, description
FROM(SELECT *, count(group_id) as size
     FROM table1 
     GROUP BY group_id) as tmp
JOIN table2
ON tmp.group_id = table2.group_id
UNION
SELECT id, 0 as size, name, description
FROM table2
But when I try to make query with codeigniter, it won't work.
Error: Unknown column '0' in 'field list'
SELECT id, 0 as size, name, description 
FROM table2
Here is the Codeigniter code from module:
$this->db->select('group_id, size, name, description');
$this->db->from('(select *, count(group_id) as size from table1 group by group_id) as tmp');
$this->db->join('table2', 'tmp.group_id=table2.id');
$this->db->get();
$query1 = $this->db->last_query(); 
$this->db->select('id, 0 as size, name, description');
$this->db->from('table2');
$this->db->get();
$query2 = $this->db->last_query(); 
$this->db->query($query1. ' UNION ' .$query2);
$query = $this->db->get();
return $query->result_array();
To make the story short I need to know how to make placeholder with Codeigniter or is there any other, better way to get the result I need?
                        
You have to escape your
selectquery by addingFALSEvalue as the second parameter of codeigniter's active recordselect()function.So, it should be written like:
When you add
FALSEvalue to the second parameter of it, codeigniter won't keep the first parameter to be initialized bybacktickcharacter to the query.