I've two tables named nodes, and links, like so:
--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
I'd like to produce a third table by matching the fromx, fromy, and tox, toy in the Links table against the x and y in the Nodes table to produce a table like so:
linkid fromid toid
--------------------
a1 1 2
a2 2 3
a3 2 4
a4 1 4
a5 1 5
In an effort to get to that result, I used this query to join twice on the nodes table using the following query, but I get no results.
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
I"m happy to create a temp table or such, if that would help.
答案 0 :(得分: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