0

我需要这方面的帮助。

我有 3 个表(FROM、TO 和 DISTANCE):

FROMs(Id, Name)
1 - London
2 - Beijing
3 - Athens

TOs(Id, Name)
1 - New York
2 - Madrid
3 - Paris

DISTANCEs(Id,FROMid,TOid,km,obs)
1 - 2 - 3 - 400 - distance between Beij. and Paris

我需要一个返回这个的查询:

1    - 2 - 3 - 400 - distance between Beij. and Paris
NULL - 1 - 1 - NULL - NULL

对于所有不存在的组合,依此类推。

谢谢。

4

1 回答 1

3

这样的事情应该做:

这是SQLFiddle的链接

select a.id, b.fromId, b.toId, a.km, a.obs from distances a
right join(
select a.id as fromId, 
       a.name as fromName,
       b.id as toId,
       b.name as toName
from froms a
join tos b
on 1=1) b
on a.fromId = b.fromId
   and a.toId = b.toId
于 2012-08-07T17:58:36.103 回答