hgu*_*ser 5 sql ms-access join
我正在使用访问权限,我有两个表:
点:
id,x,y
1 32432432 143423232
2 32432443 143423300
Run Code Online (Sandbox Code Playgroud)
线:
id startPoint endPoint
1 1 2
Run Code Online (Sandbox Code Playgroud)
现在,当我查询该行时,我希望返回的表将包含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;
Run Code Online (Sandbox Code Playgroud)
然后我可以得到以下仅包含startPoint的结果.
id startPoint endPoint x1 y1
1 1 2 ......
Run Code Online (Sandbox Code Playgroud)
那么如何在我想要这样的结果的同时检索endPoint(x2 y2是endPoint的坐标):
id startPoint endPoint x1 y1 x2 y2
1 1 2 ......
Run Code Online (Sandbox Code Playgroud)
我尝试了两个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;
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)
每个额外的连接需要在第一个表之前的另一个左paren和在倒数第二个连接之后的右paren.
| 归档时间: |
|
| 查看次数: |
1978 次 |
| 最近记录: |