我承认在这里是一个绝对的SQL Noob(我可以做内部联接,但那是关于"复杂"因为我的知识与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的人是否愿意以更好的方式为实现我的最终目标提供一些见解?任何帮助将不胜感激。感谢您提前的时间。
编辑:我知道第一个查询并不真正需要内部联接,我刚从第二个查询中复制/粘贴并且没有更改它。 >。<
答案 0 :(得分: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;
答案 1 :(得分: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
答案 2 :(得分: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将评估第一个输入,如果它为null,它将显示第二个输入。 MSDN HERE。您还可以查看COALESCE