3

我承认在这里我是一个绝对的 SQL 菜鸟(我可以进行内部连接,但这与我对 SQL 的了解一样“复杂”),但我希望获得一些关于如何最好地实现某事的帮助/见解/想法。我有一张公司表,以及这些公司的个人子表。公司可以有一个邮寄地址,但个人可能有一个不同的邮寄地址来覆盖正常的公司地址。我的环境中有 SQL Server 2008。我的想法是让 SQL 查询以下列方式循环两次

SELECT tblIndividuals.FirstName,
       tblIndividuals.LastName,
       tblIndividuals.CompanyName,
       tblIndividuals.MailingAddress1,
       tblIndividuals.MailingAddress2,
       tblIndividuals.MailingAddress3,
       tblIndividuals.MailingAddress4,
       tblIndividuals.City,
       tblIndividuals.[State],
       tblIndividuals.Zip
FROM tblIndividuals
INNER JOIN tblCompanies ON tblIndividuals.CompanyName = tblCompanies.CompanyName
WHERE tblIndividuals.ChristmasList=1
  AND tblIndividuals.MailingAddress1 IS NOT NULL
  SELECT tblIndividuals.FirstName,
         tblIndividuals.LastName,
         tblIndividuals.CompanyName,
         tblCompanies.MailingAddress1,
         tblCompanies.MailingAddress2,
         tblCompanies.MailingAddress3,
         tblCompanies.MailingAddress4,
         tblCompanies.City,
         tblCompanies.[State],
         tblCompanies.Zip
  FROM tblIndividuals
  INNER JOIN tblCompanies ON tblIndividuals.CompanyName = tblCompanies.CompanyName WHERE tblIndividuals.ChristmasList=1
  AND tblIndividuals.MailingAddress1 IS NULL
ORDER BY tblIndividuals.CompanyName

想法是这样,代码将循环一次,抓取圣诞节列表中所有具有覆盖公司地址的邮寄地址的个人,然后循环抓取圣诞节列表中使用默认公司的所有个人地址,最后按公司名称对所有结果进行排序。那些比我更精通 SQL 的人知道,这并不像我希望的那样工作,而是只运行两个查询。是否有更精通 SQL 的人愿意在这里以更好的方式提供一些见解以实现我的最终目标?任何帮助将不胜感激。提前感谢您的时间。

编辑:我知道第一个查询并不真正需要内部联接,我只是从第二个查询复制/粘贴并没有更改它。>.<

4

3 回答 3

2
select tblIndividuals.FirstName, tblIndividuals.LastName, tblIndividuals.CompanyName, 
COALESCE(tblIndividuals.MailingAddress1, tblCompanies.MailingAddress1) AS MailingAddress1, 
CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.MailingAddress2 ELSE tblIndividuals.MailingAddress2 END AS MailingAddress2, 
CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.MailingAddress3 ELSE tblIndividuals.MailingAddress3 END AS MailingAddress3, 
CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.MailingAddress4 ELSE tblIndividuals.MailingAddress4 END AS MailingAddress4, 
CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.City ELSE tblIndividuals.City END AS City, 
CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.[State] ELSE tblIndividuals.[State] END AS [State], 
CASE WHEN tblIndividuals.MailingAddress1 IS NULL THEN tblCompanies.Zip ELSE tblIndividuals.Zip END AS Zip
from tblIndividuals INNER JOIN tblCompanies
On tblIndividuals.CompanyName = tblCompanies.CompanyName
where tblIndividuals.ChristmasList=1
order by tblIndividuals.CompanyName

未来的几点。COALESCE 表示“此列表中不为空的第一个”。CASE 语句的用法应该不难弄清楚。

最后,使用 CompanyName 作为主键是个坏主意,因为 CompanyName 可能会更改。这是一个很好的数据库设计入门: http: //database-programmer.blogspot.com/2008/01/database-skills-third-normal-form-and.html

于 2012-05-11T17:46:44.787 回答
2

使用 CASE 语句。不需要两个查询,因为所有数据都在第一个......

Select tblIndividuals.FirstName, tblIndividuals.LastName, tblIndividuals.CompanyName,
    Case 
        When tblIndividuals.MailingAddress1 IS NULL then tblCompanies.MailingAddress1 
        Else tblIndividuals.MailingAddress1 END as MailingAddress1,
    Case 
        When tblIndividuals.MailingAddress1 IS NULL then tblCompanies.MailingAddress2 
        Else tblIndividuals.MailingAddress2 END as MailingAddress2,
    ....
from tblIndividuals INNER JOIN tblCompanies
On tblIndividuals.CompanyName = tblCompanies.CompanyName
where tblIndividuals.ChristmasList=1; 
于 2012-05-11T17:35:49.547 回答
0

您只需要一个

select tblIndividuals.FirstName, 
..
ISNULL(tblIndividuals.MailingAddress1, tblCompanies.MailingAddress1), 
ISNULL(tblIndividuals.MailingAddress2, tblCompanies.MailingAddress2), 
...
from tblIndividuals INNER JOIN tblCompanies
On tblIndividuals.CompanyName = tblCompanies.CompanyName
where tblIndividuals.ChristmasList=1

请注意,ISNULL 将评估第一个输入,如果它为空,它将显示第二个。MSDN 这里。你也可以看看COALESCE

于 2012-05-11T17:42:34.143 回答