Как получить все данные из базы данных из 3 таблиц? - PullRequest
0 голосов
/ 15 ноября 2018

Моя проблема в том, что я получаю данные, которые присутствуют во всех 3 таблицах. Но я хочу отобразить данные, которых нет ни в одной из таблиц (то есть пустое место или 0 напечатан на этом элементеесли его нет ни в одной из таблиц) .. Я гуглил и обнаружил, что это можно сделать с помощью 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 голосов
/ 15 ноября 2018

$ query = "SELECT Item, pgroup, SUM (Stock) AS Stock, SUM (quantity) AS quantity, SUM (Qty) AS Qty FROM opstock LEFT JOIN purchaseitem ON purchaseitem. Prdtname = opstock. Item LEFT JOIN salesitem ON salesitem. Prdtname = purchaseitem. Prdtname LEFT JOIN itemmaster ONitemmaster. itemname = purchaseitem. Prdtname LEFT JOIN pgroup ON pgroup. pgroupid = itemmaster. catcode WHERE billdate> = '". $ NewDate."'AND billdate <= '". $ NewDate2."' AND <code>billdte> = '". $ NewDate."' AND billdte <= '". $ NewDate2."' GROUP BY <code>Item ORDER BY pgroup ASC UNION SELECT Item, pgroup, SUM (Stock) AS Stock, SUM (quantity) AS quantity, SUM (Qty) AS Qty ОТ opstock ВПРАВОJOIN purchaseitem ON purchaseitem. Prdtname = opstock. Item LEFT JOIN salesitem ON salesitem. Prdtname = purchaseitem. Prdtname LEFT JOIN itemmaster ON itemmaster. itemname = purchaseitem. Prdtname LEFT JOIN pgroup ON pgroup. pgroupid = itemmaster. catcode WHERE billdate> = '". $ NewDate."' И billdate <= '". $ newDate2."' AND <code>billdte> = '". $ newDate."' AND billdte <= '". $ newDate2."' GROUP BY <code>Item ЗАКАЗАТЬ НА pgroup ASCВЫБРАТЬ Item, pgroup, СУММА (Stock) КАК Stock, СУММА (quantity) АS quantity, SUM (Qty) AS Qty ОТ opstock ВЛЕВО СОЕДИНЕНИЕ purchaseitem ВКЛ purchaseitem. Prdtname = opstock. Item ПРАВО СОЕДИНЕНИЕ salesitem ВКЛ salesitem.Prdtname = purchaseitem. Prdtname LEFT JOIN itemmaster ON itemmaster. itemname = purchaseitem. Prdtname LEFT JOIN pgroup ON pgroup. pgroupid = itemmaster.catcode WHERE billdate> = '". $ NewDate."' AND billdate <= '". $ NewDate2."' AND <code>billdte> = '". $ NewDate."' AND billdte <= '". $ newDate2."' GROUP BY <code>Item ЗАКАЗАТЬ ПО pgroup ВЫБОР СОЮЗА ASC Item, pgroup, SUM (Stock) AS Stock, SUM (quantity) AS quantity, СУММА (Qty) КАК Qty ОТ opstock ВЛЕВО СОЕДИНЯЕТСЯ purchaseitem ВКЛЮЧЕНО purchaseitem. Prdtname = opstock. Item ВЛЕВО СОЕДИНЕНО salesitem ВКЛ salesitem. Prdtname= purchaseitem. Prdtname RIGHT JOIN itemmaster ON itemmaster. itemname = purchaseitem. Prdtname LEFT JOIN pgroup ON pgroup. pgroupid = itemmaster. catcodeГДЕ billdate> = '". $ NewDate."' AND billdate <= '". $ NewDate2."' AND <code>billdte> = '". $ NewDate."' AND billdte <= '". $ newDate2. "'GROUP BY <code>Item ORDER BY pgroup ASC";

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

...