我试图在访问的sql视图中做一些复杂的连接,而且我很难知道为什么我会收到错误。我更熟练地在SQL本身中使用SQL连接,并且在提供错误时,访问的SQL视图确实令人困惑。我不知道我需要做些什么才能解决这个错误。请参阅下文。
SELECT Table1.PAT_NAME AS PATNM,
Table1.PAT_MRN_ID AS MRN,
Table2.Start_Date AS StartDT,
Table2.END_date AS ENDDT,
Table2.episode_ID AS EPSDID,
Table2.POS_NAME AS POS,
Table3.READMITDT AS READMITD
FROM Table2
LEFT JOIN ( Table1 ON Table2.PAT_MRN_ID = Table1.PAT_MRN_ID)
JOIN (Table3 ON Table1.PAT_ID = Table3.PATIDR AND
( Table3.READMITDT <= dateadd("d", 1, isnull(Table2.END_DATE,NOW())
OR Table3.READMITDT < dateadd("d", 30, Table2.START_DATE))
AND Table3.READMITDT >= Table2.start_date))
答案 0 :(得分:2)
这是一个问题......
JOIN (Table3 ON Table1.PAT_ID = Table3.PATIDR
^^^^
Access SQL不单独接受JOIN
。您必须始终指定所需的联接类型:INNER JOIN
; LEFT JOIN
;或RIGHT JOIN
。
使用查询设计器的设计视图(而不是SQL视图)来设置连接。查询设计者知道保持db引擎满意的语法规则。稍后,您可以切换回SQL View以查看有效联接的表示方式。
Access SQL将LEFT OUTER JOIN
识别为LEFT JOIN
的同义词,将RIGHT OUTER JOIN
识别为RIGHT JOIN
。在查询设计器中编辑查询时,它可能会丢弃OUTER
,但您可以在不中断工作查询的情况下将其添加回来。
考虑将这些DateAdd
条件移到查询的WHERE
子句中。
答案 1 :(得分:1)
it is better to use WHERE Clause after join operation. remove 'and'
JOIN Table3 ON (Table1.PAT_ID = Table3.PATIDR) AND
(Table3.READMITDT <= dateadd("d", 1, isnull(Table2.END_DATE,NOW())
OR Table3.READMITDT < dateadd("d", 30, Table2.START_DATE)
AND Table3.READMITDT >= Table2.start_date))
should be
JOIN Table3 ON (Table1.PAT_ID = Table3.PATIDR)
WHERE
(Table3.READMITDT <= dateadd("d", 1, isnull(Table2.END_DATE,NOW())
OR Table3.READMITDT < dateadd("d", 30, Table2.START_DATE)
AND Table3.READMITDT >= Table2.start_date))
答案 2 :(得分:0)
我对ms access sql并不熟悉。但我认为LEFT JOIN应该是LEFT OUTER JOIN并且你的括号错了。在JO子句之前的表之后应该打开Join括号 看下面的例子
FROM Table2 LEFT JOIN(Table1 ON Table2.PAT_MRN_ID = Table1.PAT_MRN_ID)应更改为
FROM Table2 LEFT OUTER JOIN Table1 ON(Table2.PAT_MRN_ID = Table1.PAT_MRN_ID)
答案 3 :(得分:0)
我通过语法检查器https://sql.treasuredata.com/运行它。此版本没有语法错误。
SELECT
Table1.PAT_NAME AS PATNM
,Table1.PAT_MRN_ID AS MRN
,Table2.Start_Date AS StartDT
,Table2.END_date AS ENDDT
,Table2.episode_ID AS EPSDID
,Table2.POS_NAME AS POS
,Table3.READMITDT AS READMITD
FROM
Table2
LEFT OUTER JOIN Table1 ON (Table2.PAT_MRN_ID = Table1.PAT_MRN_ID)
JOIN Table3 ON (Table1.PAT_ID = Table3.PATIDR) AND
(Table3.READMITDT <= dateadd("d", 1, isnull(Table2.END_DATE,NOW())
OR Table3.READMITDT < dateadd("d", 30, Table2.START_DATE)
AND Table3.READMITDT >= Table2.start_date))