【发布时间】:2015-06-11 23:37:07
【问题描述】:
我有一个计算连接表中 id 的查询。我需要将返回的表与数据库中的另一个表合并。
第一个查询返回的表有 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 进行查询时,它不起作用。
错误:“字段列表”中的未知列“0”
SELECT id, 0 as size, name, description
FROM table2
这是来自模块的codeigniter代码:
$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();
为了使故事简短,我需要知道如何使用 codeigniter 制作占位符,或者有没有其他更好的方法来获得我需要的结果?
【问题讨论】:
标签: php mysql database codeigniter union