将MySQL查询转换为MS Access

时间:2017-06-12 18:52:16

标签: mysql ms-access

我有一个非常古老的项目使用MySQL,我正在考虑将部分转换为MS Access。我遇到了一些更复杂的查询问题,并想知道是否有一个引用详细说明了Access的SQL和MySQL之间的差异。例如,我有以下查询:

my_fun

在Access中,我可以获得第一个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

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')

点击“帮助”按钮非常有用,通知我,Syntax error (missing operator) in query expression '(R.Person = P.PersonID and TookName) left join persons PR on (PR.PersonID = R.Relative.'哎呀!

但我还有一些其他相当复杂的查询,所以除了解决这个问题之外,我正在寻找可以解释一般差异的东西。

编辑:

所以,我根据链接到的答案更改了查询:

The expression you typed is not valid for the reason indicated in the message.

它告诉我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') ,并突出显示JOIN expression not supported。我还尝试了TookName=trueTookName=1。我尝试删除第二个TookName,第一个在括号中,它仍然只是告诉我JOIN

2 个答案:

答案 0 :(得分:0)

部分left join relatives R on (R.Person = P.PersonID and TookName)

似乎不完整(或者不是有效的sql表达式)

tookName与nothings无法比较

你可能需要像:

这样的事情
left join relatives R on R.Person = P.PersonID and R.TookName = P.TookName 

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

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

对于跨平台,在布尔值上你应该使用

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

或更好WHERE your_column<> 0

答案 1 :(得分:0)

Access SQL解析器是括号的忠实粉丝。

所有包含2个以上表格的JOIN都需要它们

> ds
   Visit Patient  Admission  Discharge      Daydiff Readmit30 Readmit180
1:     1       1 2015-01-01 2015-01-02      7.00000         1          1
2:     4       1 2015-01-09 2015-01-09    111.95833         0          1
3:     6       1 2015-05-01 2015-05-01 -16556.15510         0          0
4:     2       2 2015-01-01 2015-01-01     89.95833         0          1
5:     5       2 2015-04-01 2015-04-05 -16530.15510         0          0
6:     3       3 2015-01-01 2015-01-02 -16437.19677         0          0

这样只有两个表/子查询在一组括号中连接。

并且(正如我今天所知)如果你想在其中使用文字值,则需要围绕FROM (a JOIN b ON a.id = b.id) JOIN c on b.id = c.id 子句。

ON

扩展说明为here。链接在此处找到:https://stackoverflow.com/a/23632282/3820271