2个表之间的Postgresql查询

时间:2014-01-25 01:54:02

标签: postgresql join

TABLE_A中有3个字段(id1,id2,value),TABLE_B中有2个字段(id,name)。我想根据id = id1 / id = di2

获取名称而不是id1 / id2
select id1,id2,value from TABLE_A;
select name from TABLE_B where id=id1;
select name from TABLE_B where id=id2;

如何将这三个语句合并为一个,并返回如下结果:

name(id1) | name(id2) | value  

1 个答案:

答案 0 :(得分:0)

试试这个:

select c.name, b.name, a.value from TABLE_A a, TABLE_B b, TABLE_B c
where b.id = a.id1 and c.id = a.id2