select query from one table and replace all values from another table
I need to write a select query for a network diagram. There are two tables;
select * from node;
num | node
-----+------
1 | a
2 | b
3 | c
4 | d
select * from edge;
e1 | e2
----+----
a | b
c | d
a | c
I need a select statement which would give me the edge values in the form of numbers like this;
1-开发者_如何学Python2
3-4
1-3
I am using postgresql 8.4 but is should not have any relevance to my question.
Thank you.
A couple of JOINs should do the trick:
SELECT
n1.num AS num1,
n2.num AS num2
FROM node n1
JOIN edge e ON n1.node = e.e1
JOIN node n2 ON n2.node = e.e2
If you want the output as a string exactly as in your question then use this instead:
SELECT n1.num || '-' || n2.num AS nums
FROM ...
精彩评论