Translating MySQL Queries to MS Access

折月煮酒 提交于 2021-01-28 05:51:42

问题


I have a very old project which uses MySQL, which I am considering converting part of to MS Access. I'm running into problems with some of the more complex queries, and wondered if there is a reference which details the differences between Access's SQL and MySQL. For example, I have the following query:

select P.PersonID, P.FirstName, P.MiddleName, P.LastName,
       PR.LastName as MarriedName, P.Born, LocID, PlaceName,
       City, County, State, Country
from   persons P
         left join relatives R on (R.Person = P.PersonID and TookName)
         left join persons PR on (PR.PersonID = R.Relative)
         left join locations L on (L.Person = P.PersonID and L.FromDate = P.Born)
where  not P.Deleted
  and  (P.FirstName in ('Alan','Albert','Alfred','Allan','Allen','Alvin','Al')
   or   P.MiddleName in ('Alan','Albert','Alfred','Allan','Allen','Alvin','Al')
   or   P.Nickname in ('Alan','Albert','Alfred','Allan','Allen','Alvin','Al'))
  and  (P.LastName = 'Little' or PR.LastName = 'Little')
group  by P.PersonID
order  by P.Born desc

In Access, I can get as far as the first join:

select P.PersonID, P.FirstName, P.MiddleName, P.LastName,
       PR.LastName as MarriedName, P.Born
from   persons P
         left join relatives R on (R.Person = P.PersonID and TookName)
where  not P.Deleted
  and  P.FirstName in ('Alan','Albert','Alfred','Allan','Allen','Alvin','Al')

if I add the second join it says, Syntax error (missing operator) in query expression '(R.Person = P.PersonID and TookName) left join persons PR on (PR.PersonID = R.Relative.'

Clicking the Help button very helpfully informs me, The expression you typed is not valid for the reason indicated in the message. Gee thanks!

But I have some other rather complex queries, so beyond solving the problem with this one, I'm looking for something that will explain the differences in general.

EDIT:

So, I changed the query according to the answer linked to:

select P.PersonID, P.FirstName, P.MiddleName, P.LastName,
       PR.LastName as MarriedName, P.Born
from   (persons P
         left join relatives R on R.Person = P.PersonID and TookName=true)
         left join persons PR on PR.PersonID = R.Relative
where  not P.Deleted
  and  P.FirstName in ('Alan','Albert','Alfred','Allan','Allen','Alvin','Al')

It tells me JOIN expression not supported, and highlights TookName=true. I also tried it as TookName=1 and just TookName. I tried removing the second JOIN, with the first in parentheses, and it still just tells me JOIN expression not supported.


回答1:


The Access SQL parser is a huge fan of parentheses.

They are needed in all JOINs with more than 2 tables

FROM (a JOIN b ON a.id = b.id) JOIN c on b.id = c.id

so that only two tables / subqueries are joined in one set of parentheses.

And (as I learned today) they are needed around the ON clause if you want to use literal values in it.

FROM (a JOIN b ON (a.id = b.id AND b.foo = True)) JOIN c on b.id = c.id

An extended description is here. Link was found here: https://stackoverflow.com/a/23632282/3820271




回答2:


the part left join relatives R on (R.Person = P.PersonID and TookName)

seems not complete (or not a valid sql expression)

tookName is not compared with nothings

could be you need somthings like :

left join relatives R on R.Person = P.PersonID and R.TookName = P.TookName 

or

left join relatives R on R.Person = P.PersonID and R.TookName = 'FIXED VALUE'

or

left join relatives R on R.Person = P.PersonID and R.TookName is not null

for cross platform where on boolean you should use

  left join relatives R on R.Person = P.PersonID and R.TookName =1

or better WHERE your_column <> 0



来源:https://stackoverflow.com/questions/44506748/translating-mysql-queries-to-ms-access

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!