FROM子句中的带有子查询的联接操作中的SQL语法错误

时间:2018-10-25 08:19:40

标签: sql excel vba ms-access

我一直在反对这个问题,我确信这是一个非常简单的答案。在Excel 2016 VBA中,我具有以下SQL字符串,但在加入操作时遇到语法错误。

SELECT c.code, p.description, p.weight, p.[pack size], p.ppb, p.[CP-UK], p.[CP-EU]
FROM((
    SELECT *
        FROM [Catalogue Info] as c
        LEFT JOIN [Product Information] as p on c.code = p.code
    )
    LEFT JOIN [;database=C:\mypath\db.accdb;PWD=1234].tbl as pl on c.code = pl.product_code
    )
WHERE c.Category = 'VIENNOISERIE'
AND c.Sub_Cat_1 = 'CROISSANTS'
AND c.Sub_Cat_2 = ''
AND p.active = true
ORDER BY c.Page ASC, c.Page_Position ASC;

我尝试在子查询中使用第二个LEFT JOIN,但是我正在努力在SELECT子句中引用它。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

我想这会

SELECT j.code, j.description, j.weight, j.[pack size], j.ppb, j.[CP-UK], j.[CP-EU]
FROM(
    SELECT c.code, p.description, p.weight, p.[pack size], p.ppb, p.[CP-UK], p.[CP-EU],
c.Category,c.Sub_Cat_1,c.Sub_Cat_2,p.active,c.Page,c.Page_Position
        FROM [Catalogue Info] as c
        LEFT JOIN [Product Information] as p on c.code = p.code
    )j
    LEFT JOIN [;database=C:\mypath\db.accdb;PWD=1234].tbl as pl on j.code = pl.product_code

WHERE j.Category = 'VIENNOISERIE'
AND j.Sub_Cat_1 = 'CROISSANTS'
AND j.Sub_Cat_2 = ''
AND j.active = true
ORDER BY j.Page ASC, j.Page_Position ASC;

答案 1 :(得分:1)

假设您查询外部数据库的方式是正确的语法

    SELECT c.code, p.description, p.weight, p.[pack size], p.ppb, p.[CP-UK], p.[CP-EU]
        FROM [Catalogue Info] as c
        LEFT JOIN [Product Information] as p on c.code = p.code
    LEFT JOIN [;database=C:\mypath\db.accdb;PWD=1234].tbl as pl on c.code = pl.product_code
WHERE c.Category = 'VIENNOISERIE'
AND c.Sub_Cat_1 = 'CROISSANTS'
AND c.Sub_Cat_2 = ''
AND p.active = true
ORDER BY c.Page ASC, c.Page_Position ASC;

此外,为什么在.tbl as pl语句中不使用外部数据库(SELECT)时,为什么要包含它呢?

答案 2 :(得分:0)

使用以下代码

SELECT c.code, p.description, p.weight, p.[pack size], p.ppb, p.[CP-UK], p.[CP-EU]
FROM((
    SELECT *
        FROM [Catalogue Info] as c
        LEFT JOIN [Product Information] as p on c.code = p.code
    ) as t
    LEFT JOIN [;database=C:\mypath\db.accdb;PWD=1234].tbl as pl on t.code = pl.product_code
    ) as t1
WHERE t1.Category = 'VIENNOISERIE'
AND t1.Sub_Cat_1 = 'CROISSANTS'
AND t1.Sub_Cat_2 = ''
AND pactive = true
ORDER BY Page ASC, Page_Position ASC;