Codeigniter三表联接,显示图像问题

时间:2019-03-16 11:43:15

标签: php mysql database codeigniter frameworks

我正在使用CodeIgniter PHP框架和MySQL作为数据库。我要显示房间的特定平面图(img),也要显示没有房间的建筑物。

我有3张桌子:

tblbldg-建筑

  • id(pk)
  • 名称

小房间-房间

  • id
  • 名称
  • 地板
  • building_id(fk)

tblfloorplan-楼层平面图

  • id
  • 地板
  • building_id(fk)
  • img

我尝试了这段代码,但最终却混入了img,就像building1 floor1与2号楼floor1显示相同的平面图:

public function get_rooms(){
    $this->db->select('tblroom.id,
        tblroom.name,
        tblroom.floor,
        tblbldg.name as building_name,
        tblfloorplan.img');
    $this->db->from('tblroom');
    $this->db->join('tblbldg',"tblbldg.id = tblroom.building_id");
    $this->db->join('tblfloorplan',"tblfloorplan.floor = tblroom.floor");
    return $this->db->get();
}

1 个答案:

答案 0 :(得分:0)

尝试一下

public function get_rooms(){
       $this->db->select('tblroom.id,
            tblroom.name,
            tblroom.floor,
            tblbldg.name as building_name,
            tblfloorplan.img');
        $this->db->from('tblroom');
        $this->db->join('tblbldg',"tblbldg.id = tblroom.building_id");
        $this->db->join('tblfloorplan',"tblbldg.id = tblfloorplan.building_id and tblfloorplan.floor = tblroom.floor");
        return $this->db->get();
    }