如何在codeigniter中从两个具有相同列名的不同表中选择数据

oxf4rvwz  于 2021-06-25  发布在  Mysql
关注(0)|答案(3)|浏览(283)

您好,请参考我下面的代码,这是在模型

public function common_report($where) {
    $this->db->select('*');
    $this->db->from('collect_data_form_history_db as ch');
    $this->db->join('board_artwork_history_db as bh','bh.shop_id = ch.shop_id');
    $this->db->where('bh.shop_id', $where);
    // $this->db->where('bh.shop_id',$where);
    $query = $this->db->get();
    return $query->result_array();
}

我想要的是从表收集的所有数据,从表历史数据库和板历史数据库。我得到的数据格式不对。在“收集数据表历史数据库”中,我有11个条目,而在board artwork历史数据库中,我有18个条目。所以我得到的数据应该是29行,但我得到的198行意味着11*18。请告诉我解决办法

1tu0hz3e

1tu0hz3e1#

你需要用左连接。这样试试

$this->db->join('board_artwork_history_db as bh','bh.shop_id = ch.shop_id', 'left');
dhxwm5r4

dhxwm5r42#

$this->db->select('t1.*, t2.*')
          ->from('collect_data_form_history_db t1, board_artwork_history_db t2')
          ->where('t1.shop_id = t2.shop_id')
          ->where('t2.shop_id', $where);

$query = $this->db->get();
m528fe3b

m528fe3b3#

尝试完全外部连接

public function common_report($where) {
    $this->db->select('*');
    $this->db->from('collect_data_form_history_db as ch');
    $this->db->join('board_artwork_history_db as bh','bh.shop_id = ch.shop_id', 'full outer');
    $this->db->where('bh.shop_id', $where);
    // $this->db->where('bh.shop_id',$where);
    $query = $this->db->get();
    return $query->result_array();
}

相关问题