【发布时间】:2015-12-08 16:42:59
【问题描述】:
我想知道是否可以从 codeigniters 活动记录中合并如下的 sql 查询。
//get assigned contacts
$this->db->select('*, CONCAT(first_name, " " ,last_name) AS name', FALSE);
$this->db->from('customers_contacts');
$this->db->join('customers_accounts', 'customers_accounts.contact_id = customers_contacts.contact_id');
$this->db->like('CONCAT(first_name, " " ,last_name)', $q);
$results1 = $this->db->get();
//get non assigned contacts
$this->db->select('*, CONCAT(first_name, " " ,last_name) AS name', FALSE);
$this->db->from('customers_contacts');
$this->db->like('CONCAT(first_name, " " ,last_name)', $q);
$results2 = $this->db->get();
我尝试使用 $query = array_merge($results1, $results2); 但这不起作用,我相信因为 ->get() 正在返回一个对象数组。
所以我让它通过一个 foreach 循环来工作,然后合并结果数组。但我需要做一些条件,这些条件在一个 foreach 循环中比在两个循环中更容易。
【问题讨论】:
-
对不起,我应该更清楚一点,因为两个查询都在同一个表上,只有一个与联结表连接,(已分配与未分配的联系人)在已分配的联系人中返回了额外的列。所以联合是行不通的。
标签: php mysql codeigniter merge querying