MS Access等效查询

时间:2014-02-17 21:33:01

标签: mysql sql ms-access

我正在尝试编写一个我知道与MySQL兼容的查询到MS Access。这是一个编程任务,所以我不希望直接给出答案,但我不太了解MS Access的SQL版本。我自己编写了MySQL,并对其进行了测试。那时我意识到查询需要适用于MS Access。所以这是已知的代码:

SELECT `D`.`RENT_NUM`, 
    `R`.`RENT_DATE`, 
    `D`.`VID_NUM`, 
    `M`.`MOVIE_TITLE`, 
    `D`.`DETAIL_DUEDATE`, 
    `D`.`DETAIL_RETURNDATE`, 
    `D`.`DETAIL_FEE`, 
    `D`.`DETAIL_RETURNDATE` - `D`.`DETAIL_DUEDATE` AS `DAYS_LATE`
FROM `detailrental` AS `D`
    JOIN `rental` AS `R` ON `D`.`RENT_NUM` = `R`.`RENT_NUM`
    JOIN `video` AS `V` ON `D`.`VID_NUM` = `V`.`VID_NUM`
    JOIN `movie` AS `M` ON `V`.`MOVIE_NUM` = `M`.`MOVIE_NUM`
WHERE `D`.`DETAIL_RETURNDATE` - `D`.`DETAIL_DUEDATE` > 0
    ORDER BY `R`.`RENT_NUM`, `M`.`MOVIE_TITLE`;

我一直在尝试转换为MS Access SQL,但我仍然没有得到它。这是最近的尝试。

SELECT [D].[RENT_NUM],
    [R].[RENT_DATE],
    [D].[VID_NUM],
    [M].[MOVIE_TITLE],
    [D].[DETAIL_DUEDATE],
    [D].[DETAIL_RETURNDATE],
    [D].[DETAIL_FEE],
    [D].[DETAIL_RETURNDATE] - [D].[DETAIL_DUEDATE] AS [DAYS_LATE]
FROM [DETAILRENTAL] AS [D] INNER JOIN 
( 
  [RENTAL] AS [R] INNER JOIN 
  (
    [VIDEO] AS [V] INNER JOIN [MOVIE] AS [M] ON [V].[MOVIE_NUM] = [M].[MOVIE_NUM]
  )  ON [D].[VID_NUM] = [V].[VID_NUM]
) ON [D].[RENT_NUM] = [R].[RENT_NUM]
WHERE [D].[DETAIL_RETURNDATE] - [D].[DETAIL_DUEDATE] > 0
    ORDER BY [R].[RENT_NUM], [M].[MOVIE_TITLE];

我收到的错误是Syntax error in JOIN operation.我知道这意味着什么,但我不知道MS Access的SQL是否足以发现错误。

2 个答案:

答案 0 :(得分:2)

我已经有一段时间了,因为我必须编写任何访问查询,所以我只使用官方文档http://msdn.microsoft.com/en-us/library/office/bb208854(v=office.12).aspx描述的语法。弄清楚它有语法错误。因此,要进行查询工作,您必须用括号括起JOIN。但是每个ON语句仍然需要在每组括号内,而不是在外部。这应该可以解决问题。

SELECT [D].[RENT_NUM]
       ,[R].[RENT_DATE]
       ,[D].[VID_NUM]
       ,[M].[MOVIE_TITLE]
       ,[D].[DETAIL_DUEDATE]
       ,[D].[DETAIL_RETURNDATE]
       ,[D].[DETAIL_FEE]
       ,[D].[DETAIL_RETURNDATE] - [D].[DETAIL_DUEDATE] AS [DAYS_LATE]
    FROM (
           (
             (
               [detailrental] AS [D] )
             INNER JOIN [rental] AS [R]
                ON [D].[RENT_NUM] = [R].[RENT_NUM] )
           INNER JOIN [video] AS [V]
            ON D.VID_NUM = [V].[VID_NUM] )
    INNER JOIN [movie] AS [M]
        ON [V].[MOVIE_NUM] = [M].[MOVIE_NUM]
    WHERE [D].[DETAIL_RETURNDATE] - [D].[DETAIL_DUEDATE] > 0
    ORDER BY [R].[RENT_NUM]
       ,[M].[MOVIE_TITLE];

答案 1 :(得分:1)

因为我们知道问题出在JOIN上,所以让我们从最里面的括号开始,然后向外工作:

[VIDEO] AS [V] 
INNER JOIN 
[MOVIE] AS [M] 
    ON [V].[MOVIE_NUM] = [M].[MOVIE_NUM]

看起来很好。让我们走出一个层次:

[RENTAL] AS [R] INNER JOIN 
(
    [VIDEO] AS [V] 
    INNER JOIN 
    [MOVIE] AS [M] 
        ON [V].[MOVIE_NUM] = [M].[MOVIE_NUM]
)
    ON [D].[VID_NUM] = [V].[VID_NUM]

此JOIN不起作用。你能明白为什么吗?