prestashop 1.6 中具有过滤天数的休眠用户

Dormant users with filter days in prestashop 1.6

我正在尝试在 $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 是数据库异常,我得到的问题是我在这里没有看到我的前两个连接,即它没有采用前两个连接

您正在覆盖每次调用 $this->_join = 时的 _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;

我试过这种对我有用的方法:

$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';