mysql列表与查询逻辑的比较问题

时间:2010-06-26 07:43:11

标签: mysql list comparison

我正在比较mysql中的两个电子邮件地址列表 - listA和listB(两者都是视图)。最初,我想查找listA中不在listB中的所有电子邮件地址。我成功完成了这个:

$comparison_query = mysql_query("SELECT DISTINCT email_addresses FROM listA WHERE email_addresses NOT IN (SELECT DISTINCT email_addresses FROM listB) ORDER BY email_addresses");

现在,我想查找listA中不在listB中的所有电子邮件地址或异常表。我试过这样做:

$comparison_query = mysql_query("SELECT email_addresses FROM listA WHERE email_addresses NOT IN (SELECT DISTINCT email_addresses FROM ((SELECT email_addresses FROM listB) UNION (SELECT email_addresses FROM exception))) ORDER BY email_addresses");

但是,这不起作用。谁能看到我错在哪里?

谢谢!

2 个答案:

答案 0 :(得分:1)

我觉得这样的事情可以解决问题:

SELECT email_addresses FROM listA
WHERE email_addresses NOT IN
(SELECT DISTINCT email_addresses FROM FROM listB)
AND email_addresses NOT IN
(SELECT email_addresses FROM exception);

答案 1 :(得分:1)

USE AND代替UNION

 mysql_query("SELECT email_addresses FROM listA
        WHERE email_addresses  NOT IN
        ( SELECT DISTINCT email_addresses FROM listB) AND 
         email_addresses  NOT IN
        (SELECT DISTINCT email_addresses FROM exception)) 
        ORDER BY email_addresses");