左连接错误 - JOIN操作

时间:2017-08-30 09:59:02

标签: sql database ms-access left-join

我正在使用MS-ACESS 2016,使用不同的表进行查询。

此查询出错,错误是:

  

JOIN操作上的语法错误

查询:

SELECT  r.data                                                      AS data,
        r.cod_treb                                                  AS codi_treballador,
        r.cod_proj                                                  AS codi_projecte,
        r.hores                                                     AS hores_reunions, 
        Nz(ts.hores, 0)                                             AS hores_ts, 
        Iif(r.hores - hores_ts > 0, r.hores - Nz(ts.hores, 0), 0)   AS r_no_fetes 
FROM    reunions r 
        LEFT JOIN   (SELECT tl.*, j.cod_proj, ts.nom_treb, ts.acronim
                    FROM ((((timesheet_lines tl 
                    LEFT JOIN timesheets AS t
                        ON tl.timesheet_id = t.id)
                    LEFT JOIN justificacions AS j 
                        ON t.id_justificacio = j.id)
                    LEFT JOIN treballadors AS tw
                        ON r.cod_treb = tw.cod_treb)
                    LEFT JOIN projectes AS p
                        ON r.cod_proj = p.cod_proj))  AS ts
        ON r.cod_proj = ts.cod_proj
        AND r.cod_treb = ts.cod_treb  
        AND r.data = ts.data
        AND r.cod_treb = ts.cod_treb
        AND r.cod_proj = ts.cod_proj;

如果我尝试运行子查询(相同的错误)

SELECT tl.*, j.cod_proj, ts.nom_treb, ts.acronim
FROM ((((timesheet_lines tl, reunions r 
    LEFT JOIN timesheets AS t
        ON tl.timesheet_id = t.id)
    LEFT JOIN justificacions AS j 
        ON t.id_justificacio = j.id)
    LEFT JOIN treballadors AS tw
        ON r.cod_treb = tw.cod_treb)
    LEFT JOIN projectes AS p
        ON r.cod_proj = p.cod_proj)  AS ts
    WHERE r.cod_proj = ts.cod_proj
    AND r.cod_treb = ts.cod_treb  
    AND r.data = ts.data
    AND r.cod_treb = ts.cod_treb
    AND r.cod_proj = ts.cod_proj;

谢谢。

4 个答案:

答案 0 :(得分:0)

从阅读中你会遇到以下问题:

FROM reunions AS r AS 错误。

FROM(tenter code hereimesheet_lines AS tl 应为FROM timesheet_lines tl

ON j.id = t.id_justificacio) AS ts应为ON j.id = t.id_justificacio

我希望这是正确的,因为这两个(和)毫无意义。

编辑: 根据我的Access 2010,这没关系,因为Join-Error已经消失了,它说,找不到表x:

SELECT  r.data                                                      AS data,
        r.cod_treb                                                  AS codi_treballador,
        r.cod_proj                                                  AS codi_projecte,
        r.hores                                                     AS hores_reunions, 
        Nz(ts.hores, 0)                                             AS hores_ts, 
        Iif(r.hores - hores_ts > 0, r.hores - Nz(ts.hores, 0), 0)   AS r_no_fetes 
FROM    reunions r 
        LEFT JOIN   (SELECT tl.*, j.cod_proj, tw.nom_treb, p.acronim
                    FROM timesheet_lines tl 
                    LEFT JOIN timesheets AS t
                        ON tl.timesheet_id = t.id
                    LEFT JOIN justificacions AS j 
                        ON t.id_justificacio = j.id
                    LEFT JOIN treballadors AS tw
                        ON r.cod_treb = tw.cod_treb
                    LEFT JOIN projectes AS p
                        ON r.cod_proj = p.cod_proj)  AS ts
        ON r.cod_proj = ts.cod_proj
        AND r.cod_treb = ts.cod_treb  
        AND r.data = ts.data
        AND r.cod_treb = ts.nom_treb
        AND r.cod_proj = ts.cod_proj;

答案 1 :(得分:0)

可能不是答案,但我太新了评论。看起来代码被视为两个语句,错误中引用的后半部分是不完整的并且没有被执行。如果这是真的,就不可能用帖子上的格式来说明原因。

此外,这可能是我的无知,但我从未见过一个SQL语句在tbl3上读取tbl2上的JOIN tbl1。是否有可能需要在tbl3上加入tbl1在tbl3上加入tbl1而第二次加入是缺少的运算符?

答案 2 :(得分:0)

您的子查询必须有其他括号:

(SELECT tl.*, j.cod_proj, t.nom_treb, p.acronim
FROM (((timesheet_lines tl 
LEFT JOIN timesheets AS t 
ON tl.timesheet_id = t.id)
LEFT JOIN justificacions AS j 
ON t.id_justificacio = j.id)
LEFT JOIN treballadors AS t
ON r.cod_treb = t.cod_treb)
LEFT JOIN projectes AS p
ON r.cod_proj = p.cod_proj)  AS ts

答案 3 :(得分:0)

我使用ACCESS 2016的设计模式让它工作,查询现在可以工作,看起来像这样:

SELECT r.data, 
       r.cod_treb, 
       r.cod_proj, 
       r.hores                                                   AS 
       hores_reunions, 
       Nz(ts.hores, 0)                                           AS hores_ts, 
       Iif(r.hores - hores_ts > 0, r.hores - Nz(ts.hores, 0), 0) AS r_no_fetes, 
       treballadors.nom_treb, 
       projectes.acronim 
FROM   ((reunions AS r 
         LEFT JOIN (SELECT tl.*, 
                           j.cod_proj 
                    FROM   (timesheet_lines AS tl 
                            LEFT JOIN timesheets AS t 
                                   ON tl.timesheet_id = t.id) 
                           LEFT JOIN justificacions AS j 
                                  ON j.id = t.id_justificacio) AS ts 
                ON ( r.cod_proj = ts.cod_proj ) 
                   AND ( r.cod_treb = ts.cod_treb ) 
                   AND ( r.data = ts.data )) 
        INNER JOIN projectes 
                ON r.cod_proj = projectes.cod_proj) 
       INNER JOIN treballadors 
               ON r.cod_treb = treballadors.cod_treb;