我正在尝试使用左连接在$ 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异常我得到的问题是我没有看到我的前两个连接,即它没有采取前两个连接
答案 0 :(得分: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;
答案 1 :(得分: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';