Codeigniter查询两个相同的表

时间:2018-03-08 10:13:22

标签: codeigniter

我在一个数据库中有两个表。 一个表可由客户填写,一个由我们填写。这两个表具有相同的列。如何从一个foreach中的两个表中获取数据?

我有tblexample和tbluserexample。

这是我目前的获取功能:

  public function get($id = '', $where = array())
{
    $this->db->select('*,tblexample.id as exampleid');
    $this->db->from('tblexample');
    $this->db->where($where);

    if (is_numeric($id)) {
    $this->db->where('tblexample.id', $id);
    $example = $this->db->get()->row();
    if ($example) {
        $example->attachment            = '';
        $example->filetype              = '';
        $example->attachment_added_from = 0;

        $this->db->where('rel_id', $id);
        $this->db->where('rel_type', 'example');
        $file = $this->db->get('tblfiles')->row();

        if ($file) {
            $example->attachment            = $file->file_name;
            $example->filetype              = $file->filetype;
            $example->attachment_added_from = $file->staffid;
        }
    }

    return $example;
      }
     return $this->db->get()->result_array();
}

祝你好运, 贾瑞恩

1 个答案:

答案 0 :(得分:1)

是否存在任何常见字段,然后只连接两个表..如果没有,则使用array_merge()将两个数组(查询结果)合并为一个。

将第一个查询带到array1&第二个到array2然后合并并返回array2

$array2=array_merge($array2, $array1); 
相关问题