如何将 SQL 查询转换为 Access 2007?

How to convert SQL Query for Access 2007?

这是我的查询。我需要对其进行格式化,以便它可以 运行 在 Access 2007 中。

SELECT DISTINCT(meters.meterNo),  readings.momentaryIntCount, 
readings.readingDate, 
ServiceLocations.servLoc
FROM readings
inner join EndPoints on readings.endPointId = endpoints.endPointId  
join Meters on endpoints.meterId = meters.meterId
join spus on endpoints.spuid = spus.spuid
join ServiceLocations on meters.serviceLocationId = ServiceLocations.serviceLocationId
left join firmware on endpoints.firmwareId = firmware.firmwareId
    left join virtualgroupassoc v on EndPoints.endpointid = v.objectid
left join groups g on v.groupid = g.groupid and g.grouptypeid in (4, 24) and g.active = 1
WHERE readings.readingDate = '01-01-2015'
and firmware.version >= '18'
and quality = 0
and g.name is null
ORDER BY meterNo

在 Access 中连接多个表有点麻烦。您必须嵌套连接。

例如,如果您想在 SQL 中执行此操作:

SELECT 
    a.columna
    , b.columnb
    , c.columnc
FROM tablea AS a 
LEFT JOIN tableb AS b 
    ON a.id = b.id 
LEFT JOIN tablec AS c 
    ON a.id = c.id

您需要在 Access 中执行此操作:

SELECT 
    a.columna
    , b.columnb
    , c.columnc
FROM (
    (tablea AS a) 
    LEFT JOIN tableb AS b ON a.id = b.id) 
    LEFT JOIN tablec AS c ON a.id = c.id

所以对于您的查询:

SELECT 
    DISTINCT(meters.meterNo)
    , readings.momentaryIntCount
    , readings.readingDate
    , ServiceLocations.servLoc
FROM ((((((
    (readings)
    INNER JOIN EndPoints 
        ON readings.endPointId = EndPoints.endPointId)  
    JOIN Meters 
        ON EndPoints.meterId = Meters.meterId)
    JOIN spus 
        ON EndPoints.spuid = spus.spuid)
    JOIN ServiceLocations 
        ON meters.serviceLocationId = ServiceLocations.serviceLocationId)
    LEFT JOIN firmware 
        ON EndPoints.firmwareId = firmware.firmwareId)
    LEFT JOIN virtualgroupassoc v 
        ON EndPoints.endpointid = v.objectid)
    LEFT JOIN groups g 
        ON v.groupid = g.groupid AND g.grouptypeid in (4, 24) AND g.active = 1
WHERE readings.readingDate = '01-01-2015'
    and firmware.version >= '18'
    and quality = 0
    and g.name is null
ORDER BY meterNo

资源:

http://nm1m.blogspot.com/2007/10/multiple-left-joins-in-ms-access.html