我的问题是,我要获取所有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);
}
}
}
答案 0 :(得分:1)
$ query =“ SELECT Item
,pgroup
,SUM(Stock
)AS Stock
,SUM({quantity
)AS quantity
,从Qty
到Qty
的SUM(opstock
)
左联接purchaseitem
上purchaseitem
。Prdtname
= opstock
。Item
左联接salesitem
上salesitem
。Prdtname
= purchaseitem
。Prdtname
左联接itemmaster
上itemmaster
。itemname
= purchaseitem
。Prdtname
在pgroup
上左联接pgroup
。pgroupid
= itemmaster
。catcode
在billdate
> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND billdate
> ='“。$ newDate。”'AND billdte
<='“。$ newDate2。”'GROUP BY {{1 }}按billdte
ASC排序
联盟
选择Item
,pgroup
,SUM({Item
)AS pgroup
,SUM({Stock
)AS Stock
,SUM(quantity
)作为quantity
的{{1}}
在Qty
上右加入Qty
。opstock
= purchaseitem
。purchaseitem
左联接Prdtname
上opstock
。Item
= salesitem
。salesitem
左联接Prdtname
上purchaseitem
。Prdtname
= itemmaster
。itemmaster
在itemname
上左联接purchaseitem
。Prdtname
= pgroup
。pgroup
在pgroupid
> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND itemmaster
> ='“。$ newDate。”'AND catcode
<='“。$ newDate2。”'GROUP BY {{1 }}按billdate
ASC排序
选择billdate
,billdte
,SUM({billdte
)AS Item
,SUM({pgroup
)AS Item
,SUM(pgroup
)作为Stock
的{{1}}
左联接Stock
上quantity
。quantity
= Qty
。Qty
在opstock
上右加入purchaseitem
。purchaseitem
= Prdtname
。opstock
左联接Item
上salesitem
。salesitem
= Prdtname
。purchaseitem
在Prdtname
上左联接itemmaster
。itemmaster
= itemname
。purchaseitem
在Prdtname
> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND pgroup
> ='“。$ newDate。”'AND pgroup
<='“。$ newDate2。”'GROUP BY {{1 }}按pgroupid
ASC排序
联盟
选择itemmaster
,catcode
,SUM({billdate
)AS billdate
,SUM({billdte
)AS billdte
,SUM(Item
)作为pgroup
的{{1}}
左联接Item
上pgroup
。Stock
= Stock
。quantity
左联接quantity
上Qty
。Qty
= opstock
。purchaseitem
在purchaseitem
上右加入Prdtname
。opstock
= Item
。salesitem
在salesitem
上左联接Prdtname
。purchaseitem
= Prdtname
。itemmaster
在itemmaster
> ='“。$ newDate。”'和{{ 1}} <='“。$ newDate2。”'AND itemname
> ='“。$ newDate。”'AND purchaseitem
<='“。$ newDate2。”'GROUP BY {{1 }} ORDER BY Prdtname
ASC”;
$ query = $ this-> db-> query($ query);