发送电子邮件爆炸到错误的组。如何找出两个mysql查询的常见结果

时间:2015-08-19 19:55:51

标签: mysql

抱歉,我必须快速解决这个问题:(

我有两个问题:

select c.customers_email_address, c.customers_lastname, c.customers_firstname from customers c, orders o WHERE c.customers_id = o.customers_id and o.date_purchased > subdate(now(),INTERVAL 3 MONTH) GROUP BY c.customers_email_address order by c.customers_lastname, c.customers_firstname ASC LIMIT 0, 150

SELECT DISTINCT c.customers_email_address, c.customers_lastname, c.customers_firstname FROM customers c LEFT JOIN  orders o ON c.customers_id=o.customers_id JOIN orders_products op ON op.orders_id=o.orders_id WHERE op.products_id BETWEEN 378 AND 379

我原本应该使用第二个查询,但是我使用了第一个查询。我在邮寄前150张邮件后抓住了它,但现在我必须生成一个新列表,其中包含第一个查询中不会在第二个中显示的所有人,以便我可以发出道歉。

帮助?

感谢。

2 个答案:

答案 0 :(得分:1)

如果答案就像选择查询1中不在查询2中的所有记录一样简单......

select 
    c.customers_email_address, 
    c.customers_lastname, 
    c.customers_firstname 
from 
    customers c, 
    orders o 
WHERE 
    c.customers_id = o.customers_id 
    and o.date_purchased > subdate(now(),INTERVAL 3 MONTH) 
    AND c.customers_email_address NOT IN (
        SELECT DISTINCT c.customers_email_address
        FROM customers c 
        LEFT JOIN  orders o ON c.customers_id=o.customers_id 
        JOIN orders_products op ON op.orders_id=o.orders_id 
        WHERE op.products_id BETWEEN 378 AND 379
    )
GROUP BY c.customers_email_address 
order by c.customers_lastname, c.customers_firstname ASC 
LIMIT 0, 150

请注意,查询2的列选择仅减少到电子邮件地址。

答案 1 :(得分:1)

您需要做的就是选择第一个列表NOT IN第二个列表。 但是,NOT IN仅在与1列相比时才有效。因此,只选择唯一标识符。此外,当您想要完成发送讨厌的电子邮件时,您可以使用您的第二个查询并在下面添加AND NOT IN此列表。

SELECT c.customers_email_address, c.customers_lastname, c.customers_firstname
FROM customers c, orders o 
WHERE c.customers_id = o.customers_id 
AND o.date_purchased > subdate(now(),INTERVAL 3 MONTH)
AND c.customers_email_address NOT IN 
(
    SELECT DISTINCT c.customers_email_address
    FROM customers c 
    LEFT JOIN  orders o ON c.customers_id=o.customers_id 
    JOIN orders_products op ON op.orders_id=o.orders_id 
    WHERE op.products_id BETWEEN 378 AND 379
) 
GROUP BY c.customers_email_address 
ORDER BY c.customers_lastname, c.customers_firstname ASC
LIMIT 0, 150;

^看起来有趣的混合逗号连接在那里。但MySQL并不关心。