简体   繁体   中英

convert MySQL SQL to work in MS-Access Database

Trying to convert this working SQL from MySql database to work on a MS Access database:

    SELECT u.LastName AS LAST, u.FirstName AS FIRST,
    MAX(IF(`e.ClassName`='MDC (Intro)', DateCompleted, NULL)) AS 'MDC', 
    MAX(IF(`e.ClassName`='800 MHz Radio (Intro)', DateCompleted, NULL)) AS 'RADIO',
    MAX(IF(`e.ClassName`='ePCR (Intro)', DateCompleted, NULL)) AS 'ePCR',
    MAX(IF(`e.ClassName`='Firehouse (Incident)', DateCompleted, NULL)) AS 'Firehouse'
    FROM EnrollmentsTbl e INNER JOIN UsersDataTbl u ON e.UserName = u.UserName
    GROUP BY e.UserName 
    WHERE u.LastName LIKE 'Bar%' 
    ORDER BY u.LastName

A few items of syntax requires conversion:

  1. A very important item and one unfortunately MySQL users tend to abuse (which run with only full group by mode off). In SQL, GROUP BY must include ALL non-aggregated columns. So add FirstName and LastName to the grouping.
  2. MS Access SQL does not use IF for conditional expressions but IIF() .
  3. While Access does use backticks and square brackets, it can only be used to enclose column names and/or table names not both together. Come to think of it MySQL allows backticks around column and/or table names but not both in same enclosure pair (just checked in MySQL 5.5).
  4. Access does not identify column aliases with any single or double quote which the latter is the ANSI standard. Interestingly, you can include quotes but they will show up literally in column name. MySQL adheres to ANSI double quote for object identifiers with ANSI-Quotes sql mode on.
  5. Finally, Access via ODBC does use % wildcard for LIKE evaluation but Access via the GUI .exe program uses * by default. But its ALIKE operator is valid in both setup types.

Consider the following SQL adjustment:

SELECT u.LastName AS `LAST`, u.FirstName AS `FIRST`,
       MAX(IIF(e.`ClassName`='MDC (Intro)', DateCompleted, NULL)) AS `MDC`, 
       MAX(IIF(e.`ClassName`='800 MHz Radio (Intro)', DateCompleted, NULL)) AS `RADIO`,
       MAX(IIF(e.`ClassName`='ePCR (Intro)', DateCompleted, NULL)) AS `ePCR`,
       MAX(IIF(e.`ClassName`='Firehouse (Incident)', DateCompleted, NULL)) AS `Firehouse`
FROM EnrollmentsTbl e 
INNER JOIN UsersDataTbl u ON e.UserName = u.UserName
GROUP BY u.LastName, u.FirstName, e.UserName 
WHERE u.LastName ALIKE 'Bar%' 
ORDER BY u.LastName

I'm not sure if its aligned exactly as I want it (need first/last in the first column, and the GROUP BY was the main problem, but this returns results from Access:

SELECT u.LastName AS [LAST], 
Max(IIf([e.ClassName]='MDC (Intro)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [MDC], 
Max(IIf([e.ClassName]='800 MHz Radio (Intro)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [RADIO], 
Max(IIf([e.ClassName]='ePCR (Intro)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [ePCR], 
Max(IIf([e.ClassName]='Firehouse (Incident)',Format([DateCompleted],'mm/dd/yyyy'),Null)) AS [Firehouse]
FROM EnrollmentsTbl e 
INNER JOIN UsersDataTbl u ON e.UserName = u.UserName
GROUP BY u.LastName, u.FirstName, e.UserName
WHERE u.UserName LIKE 'bar%' 
ORDER BY u.LastName;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM