如何为Access 2007转换SQL查询?

时间:2015-02-20 18:12:08

标签: ms-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

1 个答案:

答案 0 :(得分:0)

在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