我有一个 2 个get
查询,最后使用 array_merge()
合并,但在此之前,我的 where
和 order_by
子句仅在第一次执行查询时实现get
仅按照 OOPS 概念。但是我希望在两个查询上实现这个where
和order_by
子句,而无需为第二个执行查询编写单独的where
和order_by
子句。我的代码如下:
$this->db->where('lower(status) <>', 'completed');
$this->db->order_by('calldate','asc');
$q1 = $this->db->get('records'); //where and orderby applies to this query only
$q2 = $this->db->get('other_records'); //where and orderby does not apply to this
$data1 = $q1->result_array();
$data2 = $q2->result_array();
$data = array_merge($data1, $data2);
试试这个
$this->db->where('lower(status) <>', 'completed');
$this->db->order_by('calldate','asc');
$q = $this->db->get('records'); //where and orderby applies to this query only
$data1 = $q->result_array();
$q = $this->db->get('other_records'); //where and orderby does not apply to this
$data2 = $q->result_array();
$data = array_merge($data1, $data2);
您可以使用 Union 编写原始 SQL。
SELECT * FROM records WHERE lower(status)<>'completed' ORDER BY calldate ASC
UNION
SELECT * FROM other_records WHERE lower(status)<>'completed' ORDER BY calldate ASC
然后,您可以避免发送 2 个单独的查询和合并数组。它减少了发送查询的网络往返和合并阵列所需的 CPU 资源。