加入三个表 - mysql

时间:2016-10-03 07:28:09

标签: php mysql codeigniter-3

基本上我有三个名为class,rooms,hostel的表。这是我的表

id  name          daily_rent    mon_rent    facilities
11  reservation     200          3000      reservation
23  per1            100          1000        per1
24  per2            100          1000        per2
25  pamba1          100          1000        pamba1
26  pamba2          100          1000        pamba2

我的桌子房间看起来像这样

    id  rm_number  capacity     bed_no                   class  hostel  is_vaccant
    34  1            5        1A,1B,1C,1D,1E               23     7      1
    35  2            4        2A,2B,2C,2D                  24     7      1
    36  3            4        3A,3B,3C,3D                  25     10     0
    37  4            6        4A,4B,4C,4D,4E,4F            26     10     0
    38  5            3        5A,5B,5C                     23     7      1
    39  6            8        6A,6B,6C,6D,6E,6F,6G,6H      25     10     1

我的宿舍表看起来像这样

    id  user_id     name    type    place   address          phone    username      password
    7   78         periyar  Girls   tirur   malappuram                 periyar  periyarhostel
    10  81         pamba    Boys    tirur   malappuram  04942569878     pamba   pambahostel

当我使用我的代码时

控制器

    public function view_vaccancy()
    {
       $data['active_mn'] = 'view_vaccancy';
       if( ! $this->ion_auth->logged_in() || !$this->ion_auth->is_admin())
         {
          redirect('login');
         }      

    $data['class'] = $this->admin_model->get_all_vaccancy_list()->result();
    $data1=array();


    foreach ($data['class'] as $row) 
    {
        $bed = $this->admin_model->bed_by_class_admin($row->id)->row();
        $res = $this->admin_model->get_admitted_room_admin($row->id);
        $data1[] = $bed->capacity-$res;
    }
    $data['available'] = $data1;

    $data['bed'] = $bed = $this->admin_model->bed_by_class_admin($row->id)->row();

    $data['form_url'] = ''; 
    $data['plc_hold'] = 'Name / ID'; 
    $this->load->view('admin/view_vaccancy',$data);
}

我的模型看起来像这样

public function get_all_vaccancy_list()
{
    $this->db->select('class.*,rooms.class,hostel.name as h_name');
    $this->db->join('rooms','class.id=rooms.class','left outer');
    $this->db->join('hostel','hostel.id=rooms.hostel');
    $query=$this->db->get('class');
    return $query;


}

我即时得到我的输出

SI no   Hostel    Class     Total Seat  No. of Vaccancy
  1     periyar   per1         12       8
  2     periyar   per2         12       4
  3     pamba     pamba1       12       8
  4     pamba     pamba2       12       2
  5     periyar   per1         12       8
  6     pamba     pamba1       12       8

但我必须得到这样的

 SI no  Hostel    Class     Total Seat  No. of Vaccancy
  1     periyar   per1         12           8
  2     periyar   per2         12           4
  3     pamba     pamba1       12           8
  4     pamba     pamba2       12           2

我的问题是模型任何猜测,以提前解决此问题

1 个答案:

答案 0 :(得分:1)

您需要对其进行分组。

$this->db->group_by('hostel, class');