我试图从我的电子邮件服务器的SQL数据库中提取联系人。 需要提取用户,联系人和所有联系人详细信息。
然而,一切都在一个单独的表中。 这就是我正在运行的:
SELECT [Contacts].[ID]
,[Users].[LoginName]
,[Contacts].[JobTitle]
,[Contacts].[Company]
,[Contacts].[WebPageAddress]
,[Contacts].[FirstName]
,[Contacts].[LastName]
,[EmailAddresses].[Address]
,[EmailAddresses].[Name]
,[Addresses].[Name]
,[Addresses].[Address1]
,[Addresses].[Town]
,[Addresses].[County]
,[Addresses].[Country]
,[Addresses].[Postcode]
,[PhoneNumbers].[Name]
,[PhoneNumbers].[Number]
FROM [WorkgroupShare].[dbo].[Contacts]
INNER JOIN [WorkgroupShare].[dbo].[Users]
ON [WorkgroupShare].[dbo].[Contacts].[Owner]=[WorkgroupShare].[dbo].[Users].[ID]
FULL OUTER JOIN [WorkgroupShare].[dbo].[EmailAddresses]
ON [WorkgroupShare].[dbo].[Contacts].[ID]= [WorkgroupShare].[dbo].[EmailAddresses].[OwnerID]
FULL OUTER JOIN [WorkgroupShare].[dbo].[Addresses]
ON [WorkgroupShare].[dbo].[Contacts].[ID]= [WorkgroupShare].[dbo].[Addresses].[OwnerID]
FULL OUTER JOIN [WorkgroupShare].[dbo].[PhoneNumbers]
ON [WorkgroupShare].[dbo].[Contacts].[ID]= [WorkgroupShare].[dbo].[PhoneNumbers].[OwnerID]
order by [Contacts].[ID]
由于EmailAddress.Name中的Name字段包含Email1或Email2,因此我会为每条记录获得双倍回报。并且Address.Name字段包含业务,家庭或其他我得到三倍。而PhoneNumbers.Name包含4个字段,我得到四倍。然后你将多个联合起来,一个6000个联系人的列表变成几万个。无法弄清楚如何编写循环或while语句。我想我的逻辑只是无法弄清楚语法。任何帮助将不胜感激。
答案 0 :(得分:1)
如果在每个“名称”字段中都有一组固定的值,则可以将它们硬编码到列中,这样每个联系人就会得到1行,其中列代表参考数据的每个“风味”。下面的内容(我没有为4个电话号码添加它,因为在这种情况下你没有为Name提供可能的值,但你应该能够得到漂移):
SELECT [Contacts].[ID]
,[Users].[LoginName]
,[Contacts].[JobTitle]
,[Contacts].[Company]
,[Contacts].[WebPageAddress]
,[Contacts].[FirstName]
,[Contacts].[LastName]
,email1.[Address] as email1Address
,email2.[Address] as email2Address
,Address1.[Address1] as HomeAddress1
,Address1.[Town] as HomeAddressTown
,Address1.[County] as HomeAddressCounty
,Address1.[Country] as HomeAddressCountry
,Address1.[Postcode] as HomeAddressPostcode
,Address2.[Address1] as BusinessAddress1
,Address2.[Town] as BusinessAddressTown
,Address2.[County] as BusinessAddressCounty
,Address2.[Country] as BusinessAddressCountry
,Address2.[Postcode] as BusinessAddressPostcode
,Address3.[Address1] as OtherAddress1
,Address3.[Town] as OtherAddressTown
,Address3.[County] as OtherAddressCounty
,Address3.[Country] as OtherAddressCountry
,Address3.[Postcode] as OtherAddressPostcode
,[PhoneNumbers].[Name]
,[PhoneNumbers].[Number]
FROM [WorkgroupShare].[dbo].[Contacts]
INNER JOIN [WorkgroupShare].[dbo].[Users]
ON [WorkgroupShare].[dbo].[Contacts].[Owner]=[WorkgroupShare].[dbo].[Users].[ID]
FULL OUTER JOIN [WorkgroupShare].[dbo].[EmailAddresses] email1
ON [WorkgroupShare].[dbo].[Contacts].[ID]= email1.[OwnerID] AND email1.[Name] = 'Email1'
FULL OUTER JOIN [WorkgroupShare].[dbo].[EmailAddresses] email2
ON [WorkgroupShare].[dbo].[Contacts].[ID]= email1.[OwnerID] AND email1.[Name] = 'Email2'
FULL OUTER JOIN [WorkgroupShare].[dbo].[Addresses] Address1
ON [WorkgroupShare].[dbo].[Contacts].[ID]= Address1.[OwnerID] AND Address1.Name = 'Home'
FULL OUTER JOIN [WorkgroupShare].[dbo].[Addresses] Address2
ON [WorkgroupShare].[dbo].[Contacts].[ID]= Address2.[OwnerID] AND Address2.Name = 'Business'
FULL OUTER JOIN [WorkgroupShare].[dbo].[Addresses] Address3
ON [WorkgroupShare].[dbo].[Contacts].[ID]= Address3.[OwnerID] AND Address3.Name = 'Other'
FULL OUTER JOIN [WorkgroupShare].[dbo].[PhoneNumbers]
ON [WorkgroupShare].[dbo].[Contacts].[ID]= [WorkgroupShare].[dbo].[PhoneNumbers].[OwnerID]
order by [Contacts].[ID]