在访问中加入语法

时间:2012-10-10 00:43:38

标签: sql ms-access join

我正在使用访问权限,我有两个表:

点:

id,x,y
1 32432432 143423232
2 32432443 143423300

行:

id startPoint endPoint
1  1          2

现在,当我查询该行时,我希望返回的表将包含startPoint的x,y和endPoint。

我尝试过加入:

select line.*,point.x as x1,point.y as y1 from line as line join point as point on line.startPoint=point.id where line.id=1;

然后我可以得到以下仅包含startPoint的结果。

id startPoint endPoint x1 y1
1  1          2        ......

然后如何在我想要结果的同时检索endPoint(x2 y2是endPoint的坐标):

id startPoint endPoint x1 y1 x2 y2
1  1          2        ......

我尝试了两个join,但它不起作用。

select line.*,point1.x as x1,point1.y as y1,point2.x as x2,point.y as y2 from line as line left join point1 as point on line.startPoint=point1.id  left join point as point2 on line.endPoint=point2.id where line.id=1;

1 个答案:

答案 0 :(得分:8)

Access具有多个连接的奇怪语法规则。你必须把它们放在括号中。

select line.*, point1.x as x1,point1.y as y1,
    point2.x as x2, point.y as y2
from (line as line
left join point as point1
on line.startPoint = point1.id)
left join point as point2
on line.endPoint = point2.id
where line.id = 1;

每个额外的连接需要在第一个表之前使用另一个左边的paren,在第二个到第二个连接之后需要一个右边的paren。