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

y3bcpkx1  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(295)
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中的date-wise从四个表中获取数据。当我使用where条件时,它只在所有表都出现的情况下获取数据,否则它不显示项。

此图片用于where条件。它仅显示所有三个表具有相同的项。

此图片不用于where条件,但它显示所有数据(如果不存在)。
请帮忙解决这个问题

flvtvl50

flvtvl501#

把where条件语句移到“on”就行了。
如。

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

如果它在哪里,这是所有上述条件的共同点。这就是为什么你没有得到任何结果。因此,将其从where条件中移除,并将该条件移动到“on”语句。

相关问题