5

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

观点:

id,x,y
1 32432432 143423232
2 32432443 143423300

线:

id startPoint endPoint
1  1          2

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

我试过加入:

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;
4

1 回答 1

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;

每个额外的连接都需要在第一个表之前有另一个左括号,在倒数第二个连接之后需要一个右括号。

于 2012-10-10T00:47:25.510 回答