1

我正在尝试使用左连接在 $this_select 中添加以下查询,但无法正常工作

以下是我的工作查询,它​​工作正常:

  select      a.id_customer as id_customer,
        a.id_shop,
        a.email,
        a.lastname,
        a.firstname,
        max(c.date_add) as last_visit,
        IFNULL(max(b.date_add),'1001-01-01 00:00:00') as Last_order_date
 from        ps_customer a
 left join  ps_orders b
  on          a.id_customer = b.id_customer
 left join   ps_guest g 
 on          a.id_customer = g.id_customer
left join   ps_connections c
on          g.id_guest = c.id_guest
group by    a.id_customer
 having      to_days(Last_order_date) < to_days(now())- '30'

但我的问题是,当我将查询代码放在控制器中时,它没有采用第一个和第二个左连接:

    $this->_select='        
    a.id_shop,
    a.email,
    a.lastname,
    a.firstname,
    max(c.date_add) as last_visit,
    IFNULL(max(b.date_add),"'.$default_date.'") as Last_order_date
    ';

    $this->_join = '
    LEFT JOIN `'._DB_PREFIX_.'orders` b ON (a.`id_customer` =b.`id_customer`)';

    $this->_join ='left join   ps_guest g 
    on (a.id_customer = g.id_customer)';

    $this->_join ='left join   ps_connections c
    ON    (   g.id_guest = c.id_guest)      
    group by    a.id_customer
    having      to_days(Last_order_date) < to_days(now())- '.$dormant_filter_days.'';

我在上面的 $this_select 或 $this_join 中做错了吗?Bleow 是 db 异常,我得到的问题是我在这里没有看到我的前两个连接,即它没有采用前两个连接

在此处输入图像描述

4

2 回答 2

3

您正在覆盖_join每次调用$this->_join =. 您应该使用$this->_join .=第二个也是最后一个加入。

$this->_select = '          
    a.id_shop,
    a.email,
    a.lastname,
    a.firstname,
    MAX(c.date_add) AS last_visit,
    IFNULL(MAX(b.date_add), "' . $default_date . '") AS Last_order_date';

$this->_join = 'LEFT JOIN `' . _DB_PREFIX_ . 'orders` b
    ON (a.`id_customer` = b.`id_customer`)';

$this->_join .= ' LEFT JOIN `' . _DB_PREFIX_ . 'guest` g 
    ON (a.id_customer = g.id_customer)';

$this->_join .= ' LEFT JOIN `' . _DB_PREFIX_ . 'connections` c
    ON (g.id_guest = c.id_guest)';

$this->_group = 'GROUP BY a.id_customer';

$this->_having = 'HAVING TO_DAYS(Last_order_date) < TO_DAYS(NOW()) - ' . $dormant_filter_days;
于 2016-07-18T12:11:58.750 回答
0

我尝试了这种对我有用的方法:

$this->_select='        
    a.id_shop,
    a.email,
    a.lastname,
    a.firstname,
    a.date_add  as registered_date,
    g.id_customer as guest_id,
    max(c.date_add) as last_visit,
    IFNULL(max(b.date_add),"'.$default_date.'") as Last_order_date
    ';
    $this->_join = '
    LEFT JOIN `'._DB_PREFIX_.'orders` b ON (a.`id_customer`   =b.`id_customer`)     
    LEFT JOIN   ps_guest g      on (a.id_customer = g.id_customer)  
    LEFT JOIN   ps_connections c
    ON    (   g.id_guest = c.id_guest)      
    ';
    $this->_where = 'group by    a.id_customer having   to_days(Last_order_date) < to_days(now())- '.$dormant_filter_days.'  AND  to_days(a.date_add) < to_days(now())- '.$dormant_filter_days.' ';
    $this->_orderBy = 'id_customer';
    $this->_orderWay = 'DESC';
于 2016-07-20T08:29:13.897 回答