如何将这两个查询合并到比较查询中

时间:2015-01-17 12:56:32

标签: php mysql

目前我有这两个问题:

"SELECT company.id, company.name, 
        contact.firstname, contact.lastname, 
        contact.email, contact.id AS contactid
   from ecampaign_lookup
   LEFT JOIN company ON ecampaign_lookup.companyid=company.id 
   LEFT JOIN contact ON ecampaign_lookup.contactid=contact.id
  WHERE ecampaign_lookup.campaignid=".$tid;

"SELECT company.id, company.name, 
         contact.firstname, contact.lastname, 
         contact.email, contact.id AS contactid
   from ecampaign_lookup
   LEFT JOIN company ON ecampaign_lookup.companyid=company.id 
   LEFT JOIN contact ON contact.companyid=company.id
  WHERE ecampaign_lookup.campaignid=".$tid." 
    AND contact.defaultcontact=1";

区别在于第一个查询从'ecampaign_lookup'表返回联系人,第二个查询从'company'表返回联系人。第二个查询还要求只选择“contact.defaultcontact = 1”的联系人,因为每个公司有很多联系人,但每个ecampaign_lookup只有一个联系人。

目前我正在分别运行这两个查询,然后使用for循环来比较结果,如果一个查询返回的次数少于或少于另一个,则容易出错:

        for ($x=0;$x<count($query1_result);$x++){
                if ($query1_result[$x]['contactid']!=$query2_result[$x]['contactid']){
                     echo $query1_result[$x]['firstname']." has been replaced by ".
                     $query2_result[$x]['firstname'];   
                }
        }   

如何将两个查询合并在一起,将ecampaign_lookup.contactid与第二个查询中的contact.id进行比较,只返回两者不匹配的结果?

2 个答案:

答案 0 :(得分:4)

要比较两个查询,UNION ALLHAVING子句,请将其作为密钥。

此示例应该有效,只返回重复的行。

SELECT id, name, firstname, lastname, email, contactid
FROM
(
  SELECT company.id, company.name, 
     contact.firstname, contact.lastname, 
     contact.email, contact.id AS contactid
  FROM ecampaign_lookup
  LEFT JOIN company ON ecampaign_lookup.companyid=company.id 
  LEFT JOIN contact ON ecampaign_lookup.contactid=contact.id
  WHERE ecampaign_lookup.campaignid=:tid

  UNION ALL

  SELECT cp2.id, cp2.name, 
     ct2.firstname, ct2.lastname, 
     ct2.email, ct2.id AS contactid
  FROM ecampaign_lookup ecl2
  LEFT JOIN company cp2 ON ecl2.companyid=cp2.id 
  LEFT JOIN contact ct2 ON ct2.companyid=cp2.id
  WHERE ecl2.campaignid=:tid AND ct2.defaultcontact=1
)
GROUP BY id, name, firstname, lastname, email, contactid
HAVING count(*) >= 1

答案 1 :(得分:0)

好的,经过多次试验和错误后,我找到了一个使用子查询的解决方案:

 SELECT company.id,
       company.NAME,
       contact.firstname,
       contact.lastname,
       contact.email,
       contact.id AS contactid
FROM   ecampaign_lookup
       LEFT JOIN company
              ON ecampaign_lookup.companyid = company.id
       LEFT JOIN contact
              ON contact.companyid = company.id
WHERE  ecampaign_lookup.campaignid = :tid
       AND contact.defaultcontact = 1
       AND contact.id NOT IN (SELECT contact.id AS contactid
                              FROM   ecampaign_lookup
                                     LEFT JOIN company
                                            ON
                                     ecampaign_lookup.companyid = company.id
                                     LEFT JOIN contact
                                            ON
                                     ecampaign_lookup.contactid = contact.id
                                     WHERE  ecampaign_lookup.campaignid = :tid)