且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

何时在 MS Access SQL 中打开和关闭连接周围的括号

更新时间:2022-12-27 18:59:44

本质上,当 MS Access 查询引用两个以上的表时,一对表之间的每个连续连接都应嵌套在括号内.

Essentially, when an MS Access query references more than two tables, every successive join between a pair of tables should be nested within parentheses.

例如,有两个表的查询不需要括号:

For example, a query with two tables requires no parentheses:

select *
from a inner join b on a.id = b.id

添加第三个连接表需要将原始连接周围的括号括起来,以便将其与附加连接区分开来:

The addition of a third joined table necessitates parentheses surrounding the original join in order to distinguish it from the additional join:

select *
from 
(
    a inner join b on a.id = b.id
) 
inner join c on a.id = c.id

表的每次连续添加都会导致现有连接嵌套在另一级括号中:

Every successive addition of a table will then cause the existing joins to be nested within another level of parentheses:

select *
from 
(
    (
        a inner join b on a.id = b.id
    ) 
    inner join c on a.id = c.id
)
inner join d on a.id = d.id

因此,一般来说:

select *
from 
(
    (
        (
            (
                table1 [inner/left/right] join table2 on [conditions]
            ) 
            [inner/left/right] join table3 on [conditions]
        )
        [inner/left/right] join table4 on [conditions]
    )
    ...
)
[inner/left/right] join tableN on [conditions]

LEFT/RIGHT 连接有一个微妙之处,因为嵌套的顺序必须保持连接的方向,例如:

There is a subtlety where LEFT/RIGHT joins are concerned, in that the order of nesting must maintain the direction of the join, for example:

select *
from 
(
    c left join b on c.id = b.id
) 
left join a on a.id = b.id

可以置换为:

select *
from 
c left join
(
    b left join a on b.id = a.id
)
on c.id = b.id