Codeiginter中如何根据日期从3个表中获取数据?

时间:2018-11-20 09:19:16

标签: php mysql codeigniter 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')) {
        $query1 = $this->db->query("SELECT `pgroup`,`Item`, SUM(`Stock`) AS `Stock`,SUM(`quantity`) AS `quantity`,SUM(`Qty`) AS `Qty`FROM `opstock` LEFT OUTER JOIN `salesitem` ON `salesitem`.`Prdtname` = `opstock`.`Item` LEFT OUTER JOIN `itemmaster` ON `itemmaster`.`itemname` = `opstock`.`Item` LEFT OUTER JOIN `pgroup` ON `pgroup`.`pgroupid` = `itemmaster`.`catcode` LEFT OUTER JOIN `purchaseitem` ON `purchaseitem`.`Prdtname` = `opstock`.`Item` WHERE  billdte >= '$newDate' AND billdte <= '$newDate2' AND billdate >= '$newDate' AND billdate <= '$newDate2' GROUP BY `Item` ORDER BY `pgroup` ASC ")->result_array();

        $data['query'] = $query1;

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

    }

    if ($this->input->post('selected')) {
        if ($name = $this->input->post('businessType')) {
            $query1 = $this->db->query("SELECT `pgroup`,`Item`, SUM(`Stock`) AS `Stock`,SUM(`quantity`) AS `quantity`,SUM(`Qty`) AS `Qty`FROM `opstock` LEFT OUTER JOIN `salesitem` ON `salesitem`.`Prdtname` = `opstock`.`Item` LEFT OUTER JOIN `itemmaster` ON `itemmaster`.`itemname` = `opstock`.`Item` LEFT OUTER JOIN `pgroup` ON `pgroup`.`pgroupid` = `itemmaster`.`catcode` LEFT OUTER JOIN `purchaseitem` ON `purchaseitem`.`Prdtname` = `opstock`.`Item` WHERE `pgroup` = '$name' AND billdte >= '$newDate' AND billdte <= '$newDate2' AND billdate >= '$newDate' AND billdate <= '$newDate2' GROUP BY `Item` ORDER BY `pgroup` ASC")->result_array();

            $data['query'] = $query1;

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

        }

    }
}

这是一个控制器代码.....

我的问题是如何在Codeiginter中使用日期明智地从四个表中获取数据。 当我使用where条件时,如果所有表都存在,则仅获取数据,否则不显示该项。enter image description here

此图片用于condition.it,它仅显示所有三个表具有相同的项目。enter image description here

此图片未在任何条件下使用,但它显示了所有数据(如果不存在)。

请帮助解决此问题

1 个答案:

答案 0 :(得分:2)

将条件语句移到“ ON”将起作用。

例如。

LEFT OUTER JOIN `salesitem` ON `salesitem`.`Prdtname` = `opstock`.`Item` AND billdte >= '$newDate' AND billdte <= '$newDate2' AND billdate >= '$newDate' AND billdate <= '$newDate2' 

如果它在where中,这对于上述所有条件都是相同的。这就是为什么您没有得到任何结果的原因。因此,将其从条件中删除,然后将该条件移至“ ON”语句。