如何连接两个表,显示表A中不存在表A的所有记录

时间:2014-05-12 03:29:24

标签: sql

我有一个电子邮件营销网络应用程序。我想显示(表B)中哪些电子邮件联系人未显示在EmailContacts_Campaign(表A)中。另外,我想通过CampaignId字段过滤表A.当我运行下面的代码时,我得到0条记录,但我知道那里有几千条记录。谁能告诉我在哪里弄乱?

SELECT * FROM TableA
LEFT JOIN TableB 
ON TableA.EmailContactId = TableB.EmailContactId
WHERE TableA.CampaignId = 1 
AND TableB.EmailContactId IS NULL
ORDER BY TableB.EmailContactId DESC

我想显示EmailContact表中未显示在EmailContactCampaign表格中的所有电子邮件联系人。这是实际的代码:

public List<EmailContact> GetNotAssignedContactsForCampaign(int campaignId)
        {
            string sqlCommand = "SELECT * FROM EmailContactCampaign LEFT JOIN EmailContact";
            sqlCommand += " ON EmailContactCampaign.EmailContact_EmailContactId = EmailContact.EmailContactId";
            sqlCommand += " WHERE EmailContactCampaign.EmailContact_EmailContactId = " + campaignId.ToString() AND EmailContact.EmailContactId IS NULL ;
            sqlCommand += " ORDER BY EmailContact.EmailContactId DESC";
            var emailContacts = new List<EmailContact>();

            string CS = db.Database.Connection.ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                con.Open();
                SqlCommand cmd = new SqlCommand(sqlCommand, con);
                //Create sql datareader
                using (SqlDataReader sqlDataReader = cmd.ExecuteReader())
                {
                    while (sqlDataReader.Read())
                    {
                        var emailContact = new EmailContact();
                        emailContact.Assigned = ((bool)sqlDataReader["Assigned"]);
                        emailContact.Cell1 = _crypto.DecryptAndSanitize(sqlDataReader["Cell1"] as string);
                        emailContact.Cell2 = _crypto.DecryptAndSanitize(sqlDataReader["Cell2"] as string);
                        emailContact.City = _crypto.DecryptAndSanitize(sqlDataReader["City"] as string);
                        emailContact.Company = _crypto.DecryptAndSanitize(sqlDataReader["Company"] as string);
                        emailContact.EmailAddress = _crypto.DecryptAndSanitize(sqlDataReader["EmailAddress"] as string);
                        emailContact.EmailContactId = (int)sqlDataReader["EmailContactId"];
                        emailContact.FullName = _crypto.DecryptAndSanitize(sqlDataReader["FullName"] as string);
                        emailContact.Hold = (bool)sqlDataReader["Hold"];
                        emailContact.Phone1 = _crypto.DecryptAndSanitize(sqlDataReader["Phone1"] as string);
                        emailContact.Phone2 = _crypto.DecryptAndSanitize(sqlDataReader["Phone2"] as string);
                        emailContact.State = _crypto.DecryptAndSanitize(sqlDataReader["State"] as string);
                        emailContact.Status = (Status)sqlDataReader["Status"];
                        emailContact.Zip = _crypto.DecryptAndSanitize(sqlDataReader["Zip"] as string);
                        emailContacts.Add(emailContact);
                    }

                }
                return (emailContacts);
            }
        }

5 个答案:

答案 0 :(得分:0)

你试过这个吗?

SELECT * FROM tableB WHERE EmailContactId NOT IN (SELECT EmailContactId FROM tableA)

答案 1 :(得分:0)

我认为你可能因为AND TableB.EmailContactId IS NULL

而得到0

请尝试这个

SELECT * FROM TableA
LEFT JOIN TableB 
ON TableA.EmailContactId = TableB.EmailContactId
WHERE TableA.CampaignId = 1
ORDER BY TableB.EmailContactId DESC

答案 2 :(得分:0)

对不起,我的问题不够明确。做了一些挖掘并在另一篇文章中找到答案。对不起,但我不小心把它关了,再也找不到了。无论如何,这是我的实现。

SELECT * FROM EmailContact
WHERE NOT EXISTS
(SELECT * FROM EmailContactCampaign WHERE EmailContactCampaign.EmailContact_EmailContactId = EmailContact.EmailContactId AND EmailContactCampaign.Campaign_CampaignId = 1)

答案 3 :(得分:0)

如果我正确地理解了你的问题,你正在寻找不在A中的B.但是你的查询将返回不在B中的A.转动它(tableB left join tableA where a... is NULL

答案 4 :(得分:0)

您的问题是您错误地解决了问题:您的查询会返回EmailContactCampaign内不在EmailContact内的所有联系人。

您问题的正确解决方案如下所示:

SELECT * FROM EmailContact
WHERE EmailContactId NOT IN (
  SELECT EmailContact_EmailContactId FROM EmailContactCampaign
  WHERE Campaign_CampaignId = ?
)
ORDER BY EmailContact.EmailContactId DESC