【发布时间】:2019-04-19 06:20:14
【问题描述】:
【问题讨论】:
-
这两个表之间有什么关系吗?
-
我不想比较这些表格
-
我的方式是分别获取记录然后合并结果
标签: mysql sql codeigniter
【问题讨论】:
标签: mysql sql codeigniter
使用union
select name from tableA
union
select name from tableB
【讨论】:
试试这个,
$tableA = $this->db->select('name')->get_compiled_select('tableA');
echo $tableA;
$tableB = $this->db->select('name')->get_compiled_select('tableB');
echo $tableB;
$tableAB = $this->get_compiled_select($tableA.' UNION '.$tableB);
echo $tableAB;
$query = $this->db->query($tableAB);
输出:
SELECT name FROM tableA
SELECT name FROM tableB
SELECT name FROM tableA UNION SELECT name FROM tableB
或者:
$query = $this->db->query("SELECT name FROM tableA UNION SELECT name FROM tableB");
【讨论】:
试试这个
$result1 = $this->db->get('TableA');
$result2 = $this->db->get('TableB');
如果您只想合并特定列,请使用select()
$this->db->select('name');
$result1 = $this->db->get('TableA');
$this->db->select('name');
$result2 = $this->db->get('TableB');
现在合并这两条记录
$combine = array_merge($result1, $result2);
阅读更多关于array_merge()
【讨论】:
select Name from TableA Union select Name from TableB
【讨论】: