2

我正在尝试使用 Recover Cart Sales 贡献来优化旧 osCommerce 网站上的一些非常慢的查询。

我正在尝试从购物车和客户表中提取信息(我可以轻松完成)。诀窍是我需要弄清楚他们最后一次联系的时间(无需进行其他查询)。那存在于另一个表中,如果我们以前从未联系过他们,则表中没有实体,因此它应该为空。

这就是我所拥有的,它的工作原理是,它只在scart 表中有条目时才提取数据。

SELECT DISTINCT cb.customers_id, c.customers_firstname, c.customers_lastname, c.customers_email_address, sc.datemodified AS last_contacted
    FROM customers_basket cb, customers c, scart sc
        WHERE c.customers_id = cb.customers_id
            AND cb.customers_id = sc.customers_id 
            AND cb.customers_basket_date_added < 20130916 
            AND cb.customers_basket_date_added > 20130101
        AND cb.customers_id NOT IN(SELECT sc.customers_id  //Excludes people we've contacted in the last month
                                FROM scart sc
                        WHERE sc.datemodified >20130816)
    ORDER BY cb.customers_id DESC
4

1 回答 1

1

听起来您需要 LEFT JOIN?如果它存在,它将返回您感兴趣的行,如果不存在,它将返回 NULL。

SELECT DISTINCT cb.customers_id, c.customers_firstname, c.customers_lastname, c.customers_email_address, sc.datemodified AS last_contacted
FROM customers_basket cb 
INNER JOIN customers c ON c.customers_id = cb.customers_id
LEFT JOIN scart sc ON cb.customers_id = sc.customers_id
WHERE cb.customers_basket_date_added < 20130916 
  AND cb.customers_basket_date_added > 20130101
  AND cb.customers_id NOT IN(SELECT sc.customers_id  //Excludes people we've contacted in the last month
                            FROM scart sc
                    WHERE sc.datemodified >20130816)
ORDER BY cb.customers_id DESC
于 2013-09-16T20:21:14.913 回答