2017-12-18 175 views
2

我命名节点的两个表格和链接,就像这样:内侧连接两次返回任何结果

--Links: 
----------------------------------- 
id fromx fromy tox  toy 
----------------------------------- 
a1  x1  y1  x2  y2 
a2  x2  y2  x3  y3 
a3  x2  y2  x4  y4 
a4  x1  y1  x4  y4 
a5  x1  y1  x5  y5 

--Nodes: 
id  x y 
-------------- 
1  x1 y1 
2  x2 y2 
3  x3 y3 
4  x4 y4 
5  x5 y5 

我想通过匹配fromx,弗罗米和TOX,玩具生产第三个表在打击的节点表中的X和Y链接表产生一个表像这样:

linkid fromid toid 
-------------------- 
    a1  1  2 
    a2  2  3 
    a3  2  4 
    a4  1  4 
    a5  1  5 

在努力达到那个结果,我用这个查询使用上的节点表连接两次以下查询,但我没有得到任何结果。

select links.id as linkid, 
n1.id as nodeid, fromx, fromy, tox from links 
inner join nodes n1 
inner join nodes n2 
on 
links.fromx = n1.x 
and links.fromy = n1.y 
and links.tox = n2.x 
and links.toy = n2.y 

我“很高兴来创建临时表或者这样,如果这将有助于。

+0

这是一个有趣的问题。我重新创建你的数据,但我现在只能访问SQL Server,因此我会尽量使用尽可能标准的语法。 –

+0

正如其他人所建议的那样,它很可能与您的数据有关。如果运行命令行SQLite shell(sqlite3。 exe文件),你可以使用“.dump”来生成SQL语句率全部创建表并插入语句。如果您将该输出添加到您的问题。其他人可以重现你的模式和数据。 – Fabian

回答

3
select 
     l.id as link_id, 
     frm.id as from_id, 
     t.id as to_id 
from 
     links l 
inner join 
     nodes frm 
     on frm.x = l.fromx 
     and frm.y = l.fromy 
inner join 
     nodes t 
     on t.x = l.tox 
     and t.y = l.toy 

SQL Fiddle

+0

非常感谢!这不幸的是不在sqlite中工作,但我不知道为什么。 (我正在使用SQLiteStudio来测试它)。运行时查询不会返回任何内容。 :( –

+0

我在SQL Server中使用了基本相同的查询,并得到了正确的结果 –

+0

@PatJones我完全理解,jophab也发布了一个链接到SQL Fiddle,它在MySql上运行正常,我认为它必须与SQLite那么? –