简体   繁体   English

LEFT JOIN没有输出期望的结果

[英]LEFT JOIN does not output desired result

I have here my sample query using codeigniter. 我在这里使用codeigniter查询示例。 I'm using this to get data satisfying my where condition. 我正在使用它来获取满足where条件的数据。

MODEL: 模型:

    function quarterly1_sales($year_date, $q1_start){
     $where = array(
     'status'   => 'purchased',
     'payment.paymentdate >=' => $q1_start,
     'payment.paymentdate <=' => 'DATE_ADD('.$q1_start.',INTERVAL 1 QUARTER)');
    return $this->db
    ->select('COUNT(payment.keyid) AS rec_count')
    ->select('product_key.client_name, product_key.contact_email, product_key.status, product_key.id, payment.paymentdate, (payment.id) as pid,payment.subscription_type')
    ->from('product_key')
    ->where($where)
    ->join('payment', 'payment.keyid=product_key.id', 'left outer')
    ->group_by('product_key.id')
    ->get()
    ->result(); 
    }

CONTROLLER: 控制器:

if($data['filter'] == 'quarterly'){
//1st quarter of the year
    @$q1_start = date('Y-m-d', strtotime(date($year_date).'-01-01'));
$data['quarterly1_sales'] = $this->basemodel->quarterly1_sales($year_date, $q1_start);
$this->load->view('sales', $data);          
}

VIEW: 视图:

if($filter == 'quarterly'){
    $title = "Quarterly Sales Summary for ".$date_year;

    echo "<br /><strong>".$title."</strong></div>
        </tr>"<tr>
    <th>Name</th>
    <th>Email Address</th>
    <th>Payment Date</th>
    <th>Subtotal Amount</th>

"; “;

    foreach ($quarterly1_sales as $quarter1) :

        echo $client_name = $quarter1->client_name;
        echo $paymentdate = $quarter1->paymentdate;
        echo $email = $quarter1->contact_email;
        echo $recount= $quarter1->rec_count;
        echo $sum_amount = $recount * 588;
    if($recount == 0){
        $display = "display:none;";
    }else{
        $display = "";
    }

endforeach; endforeach;

My problem is, there shouldn't be any result since my data starts with 2012 and 2013 yet it outputs everything. 我的问题是,应该不会有任何结果,因为我的数据以2012和2013开头,但它会输出所有内容。 Hope someone could help me on this. 希望有人可以帮助我。 Thank you. 谢谢。

Codeigniter's active record class automagically escapes everything passed to the where() function. Codeigniter的活动记录类自动删除传递给where()函数的所有内容。 Because of this your call to the MySQL function DATE_ADD() is being passed in as a string and treated literally by MySQL instead of being evaluated. 因此,您对MySQL函数DATE_ADD()调用将作为字符串传递,并由MySQL逐字处理,而不是被评估。 To fix this, move that where clause to a separate function call and set the third parameter to FALSE, turning off the escaping. 要解决此问题,请将where子句移至单独的函数调用,然后将第三个参数设置为FALSE,关闭转义。

$where = array(
     'status'   => 'purchased',
     'payment.paymentdate >=' => $q1_start);
    return $this->db
    ->select('COUNT(payment.keyid) AS rec_count')
    ->select('product_key.client_name, product_key.contact_email, product_key.status, product_key.id, payment.paymentdate, (payment.id) as pid,payment.subscription_type')
    ->from('product_key')
    ->where($where)
    ->where('payment.paymentdate <=', 'DATE_ADD('.$this->db->escape($q1_start).',INTERVAL 1 QUARTER)', FALSE)
    ->join('payment', 'payment.keyid=product_key.id', 'left outer')
    ->group_by('product_key.id')
    ->get()
    ->result(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM