如何从3个表中获取数据库中的所有数据?

时间:2018-11-15 12:14:32

标签: php mysql codeigniter full-outer-join

我的问题是,我要获取所有3个表中都存在的数据。但是我想显示任何一个表中都没有的数据(在该项目上打印为空白或0如果它不在表的任何人中..i谷歌搜索,发现它可以通过fullouter join来完成,但是我不知道如何使用它。 控制器代码:

   public function St_statement(){

        $startdate = $this->input->post('SDate');
        $enddate = $this->input->post('EDate');
        $date = str_replace('/', '-', $startdate);
        $newDate = date("Y-m-d", strtotime($date));
        $date2 = str_replace('/', '-', $enddate);
        $newDate2 = date("Y-m-d", strtotime($date2));
        $data['startdate'] = $startdate;
        $data['enddate'] = $enddate;
        if ($this->input->post('all'))
        {
        $this->db->where('billdate >=', $newDate);
        $this->db->where('billdate <=', $newDate2);
        $this->db->where('billdte >=', $newDate);
        $this->db->where('billdte <=', $newDate2);
        $this->db->select("Item");
        $this->db->select("pgroup");
        $this->db->select_sum("Stock");
        $this->db->select_sum("quantity");
        $this->db->select_sum("Qty");
        $this->db->from('opstock');
        $this->db->group_by("Item");
        $this->db->order_by("pgroup",'asc');

        $this->db->join('purchaseitem',' purchaseitem.Prdtname = opstock.Item','OUTER');
         $this->db->join('salesitem','salesitem.Prdtname =  purchaseitem.Prdtname','OUTER');
          $this->db->join('itemmaster','itemmaster.itemname =  purchaseitem.Prdtname','OUTER');
         $this->db->join('pgroup','pgroup.pgroupid = itemmaster.catcode','OUTER');

        $query = $this->db->get();
  $data['query'] = $query;

        $this->load->view('Inventory/St_Stmt', $data);
        //$this->load->view('Inventory/St_Stmt1', $data);
    }
    if($this->input->post('selected'))
    {
        if($name = $this->input->post('businessType'))
        {
        $this->db->where('billdate >=', $newDate);
        $this->db->where('billdate <=', $newDate2);
        $this->db->where('billdte >=', $newDate);
        $this->db->where('billdte <=', $newDate2);
        $this->db->where('pgroup',$name);
        $this->db->select("Item");
        $this->db->select("pgroup");
        $this->db->select_sum("Stock");
        $this->db->select_sum("quantity");
        $this->db->select_sum("Qty");
        $this->db->from('opstock');
        $this->db->group_by("Item");

        $this->db->join('purchaseitem',' purchaseitem.Prdtname = opstock.Item','OUTER');
         $this->db->join('salesitem','salesitem.Prdtname =  purchaseitem.Prdtname','OUTER');
          $this->db->join('itemmaster','itemmaster.itemname =  purchaseitem.Prdtname','OUTER');
         $this->db->join('pgroup','pgroup.pgroupid = itemmaster.catcode','OUTER');

        $query = $this->db->get();
  $data['query'] = $query;

        $this->load->view('Inventory/St_Stmt', $data);


    }

}
}

1 个答案:

答案 0 :(得分:1)

$ query =“ SELECT Itempgroup,SUM(Stock)AS Stock,SUM({quantity)AS quantity,从QtyQty的SUM(opstock) 左联接purchaseitempurchaseitemPrdtname = opstockItem 左联接salesitemsalesitemPrdtname = purchaseitemPrdtname 左联接itemmasteritemmasteritemname = purchaseitemPrdtnamepgroup上左联接pgrouppgroupid = itemmastercatcodebilldate> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND billdate> ='“。$ newDate。”'AND billdte <='“。$ newDate2。”'GROUP BY {{1 }}按billdte ASC排序 联盟 选择Itempgroup,SUM({Item)AS pgroup,SUM({Stock)AS Stock,SUM(quantity )作为quantity的{​​{1}} 在Qty上右加入Qtyopstock = purchaseitempurchaseitem 左联接PrdtnameopstockItem = salesitemsalesitem 左联接PrdtnamepurchaseitemPrdtname = itemmasteritemmasteritemname上左联接purchaseitemPrdtname = pgrouppgrouppgroupid> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND itemmaster> ='“。$ newDate。”'AND catcode <='“。$ newDate2。”'GROUP BY {{1 }}按billdate ASC排序 选择billdatebilldte,SUM({billdte)AS Item,SUM({pgroup)AS Item,SUM(pgroup )作为Stock的{​​{1}} 左联接Stockquantityquantity = QtyQtyopstock上右加入purchaseitempurchaseitem = Prdtnameopstock 左联接Itemsalesitemsalesitem = PrdtnamepurchaseitemPrdtname上左联接itemmasteritemmaster = itemnamepurchaseitemPrdtname> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND pgroup> ='“。$ newDate。”'AND pgroup <='“。$ newDate2。”'GROUP BY {{1 }}按pgroupid ASC排序 联盟 选择itemmastercatcode,SUM({billdate)AS billdate,SUM({billdte)AS billdte,SUM(Item )作为pgroup的{​​{1}} 左联接ItempgroupStock = Stockquantity 左联接quantityQtyQty = opstockpurchaseitempurchaseitem上右加入Prdtnameopstock = Itemsalesitemsalesitem上左联接Prdtnamepurchaseitem = Prdtnameitemmasteritemmaster> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND itemname> ='“。$ newDate。”'AND purchaseitem <='“。$ newDate2。”'GROUP BY {{1 }} ORDER BY Prdtname ASC”;

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