0
我有连接表中的ID计数的查询。我需要做一个返回的表与数据库中的另一个表的联合。在select语句中的Codeigniter占位符(选择ID,0作为占位符,...)
表从第一查询返回的有4列:
-group_id
-count
-name
-description
我想第一个统一的表格有3列:
-group_id
-name
-description
我通过phpmyadmin运行查询和它的工作完美。查询:
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
但是当我尝试使用codeigniter进行查询时,它将无法工作。
Error: Unknown column '0' in 'field list'
SELECT id, 0 as size, name, description
FROM table2
下面是从模块笨代码:
$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();
为了使长话短说,我需要知道如何使占位符笨或有任何其他更好的方式来获得我需要的结果是什么?