列别名与MS Access SQL中的表名冲突

时间:2018-03-30 09:06:16

标签: sql ms-access

我在 MS Access 2010数据库上工作,我偶然发现列别名的奇怪问题。我有一个多个连接的查询,我想命名我的结果列。但是,某些别名会导致空列。

在我努力理解这个问题并将其隔离的过程中,我准备了一个查询,它应该显示具有不同别名的相同列,每个别名都是我数据库中现有表或查询的名称。有趣的是,其中3个会产生空列,(AS [Analyst]AS [MeetingCompany]AS [Sector]),而其他列工作正常。请注意,Analyst表即使在此查询中也不存在,但存在于数据库中。

对于我不知道的列别名是否有任何规则?或者这只是一个奇怪的事我必须忍受并使用不同的别名?

SELECT  Sector.SectorName AS [Analyst], Sector.SectorName AS [vCompanyPrimary], 
Sector.SectorName AS [MeetingCompany], Sector.SectorName AS [Client],
Sector.SectorName AS [ClientContact], Sector.SectorName AS [MeetingClientContact], 
Sector.SectorName AS [Sector], Sector.SectorName AS [vMeetingSectorID], 
Sector.SectorName AS [EventTypeGroup], Sector.SectorName AS [Country], 
Sector.SectorName AS [Area], Sector.SectorName AS [City]
FROM (((((((((((Meeting INNER JOIN Event ON Meeting.EventID = Event.EventID)
 INNER JOIN EventType ON Event.EventTypeID = EventType.ID)
 LEFT JOIN vMeetingSectorID ON Meeting.MeetingID = vMeetingSectorID.MeetingID)
 LEFT JOIN Sector  ON vMeetingSectorID.SectorID = Sector.ID)
 LEFT JOIN City ON City.ID = Meeting.CityID)
 LEFT JOIN Area ON City.AreaID = Area.ID)
 LEFT JOIN Country ON Country.ID = City.CountryID)
 LEFT JOIN EventTypeGroup ON EventType.ID = EventTypeGroup.EventTypeID)
 LEFT JOIN MeetingClientContact ON Meeting.MeetingID = MeetingClientContact.MeetingID)
 LEFT JOIN ClientContact ON MeetingClientContact.ClientContactID = ClientContact.ContactID)
 LEFT JOIN Client ON ClientContact.ClientID = Client.ClientID)

修改 我设法找到并删除了罪魁祸首,但我仍然不理解这个限制的规则。 vMeetingSectorID是Access中保存的查询,它使用表AnalystSectorMeetingCompany。在此保存的查询中使用别名表名后,所有列都会正确显示。我会很高兴任何提示,所以我将来可以避免这些情况。

1 个答案:

答案 0 :(得分:1)

正如您所发现的那样,您遇到名称冲突,可能会产生意外结果。为了避免以后总是使用表别名,特别是使用自联接和连接查询与相同的基础表。具体来说,嵌入式查询:

LEFT JOIN vMeetingSectorID 
   ON Meeting.MeetingID = vMeetingSectorID.MeetingID

可以说相当于子查询或派生表:

LEFT JOIN (SELECT * FROM ... INNER JOIN ...) AS vMeetingSectorID 
  ON Meeting.MeetingID = vMeetingSectorID.MeetingID

此内部查询使用与外部查询相同的表,例如 Sector 。然后,引擎可能会出现问题,将标识符与名为 Sector 的结果列关联起来,作为表格。

因此,请使用正确定义内部和外部查询范围的表别名。此外,它使长查询更紧凑和可读,如下所示。当然,使用查询 vMeetingSectorID

SELECT  s.SectorName AS [Analyst], s.SectorName AS [vCompanyPrimary], 
        s.SectorName AS [MeetingCompany], s.SectorName AS [Client],
        s.SectorName AS [ClientContact], s.SectorName AS [MeetingClientContact], 
        s.SectorName AS [Sector], s.SectorName AS [vMeetingSectorID], 
        s.SectorName AS [EventTypeGroup], s.SectorName AS [cnty.], 
        s.SectorName AS [Area], s.SectorName AS [City]
FROM (((((((((((Meeting m INNER JOIN Event e ON m.EventID = e.EventID)
INNER JOIN EventType et. ON e.EventTypeID = et.ID)
LEFT JOIN vMeetingSectorID v ON m.MeetingID = v.MeetingID)
LEFT JOIN Sector s ON v.SectorID = s.ID)
LEFT JOIN City cy ON cy.ID = m.CityID)
LEFT JOIN Area a ON cy.AreaID = a.ID)
LEFT JOIN Country n ON n.ID = cy.ID)
LEFT JOIN EventTypeGroup etg ON et.ID = etg.EventTypeID)
LEFT JOIN MeetingClientContact mcc ON m.MeetingID = mcc.MeetingID)
LEFT JOIN ClientContact cc ON mcc.ClientContactID = cc.ContactID)
LEFT JOIN Client c ON cc.ClientID = c.ClientID)