oracle sql用名称替换ID列

时间:2014-01-24 06:34:56

标签: sql oracle

我有以下两个表:

命名表

 M_ID     Name
----------------
 1      apple
 2      orange
 3      pear

mapping_table

 M_ID_1    M_ID_2
------------------
  1         2
  3         1
  2         3

如何让它像那样输出??????我用的是oracle 11g。

 Name1     Name2
 ----------------
 apple      orange
 pear       apple
 orange     pear

4 个答案:

答案 0 :(得分:1)

这样的事情似乎就是你所追求的

SELECT n1.name name1,
       n2.name name2
  FROM mapping_table m
       JOIN name_table n1
         ON( m.m_id_1 = n1.m_id )
       JOIN name_table n2
         ON( n.m_id_2 = n2.m_id )

答案 1 :(得分:1)

尝试此查询:

SELECT (SELECT n.name 
        FROM   name_table n 
        WHERE  m.m_id_1 = n.m_id) name1, 
       (SELECT n.name 
        FROM   name_table n 
        WHERE  m.m_id_2 = n.m_id) name2 
FROM   mapping_table m; 

答案 2 :(得分:0)

试试这个,

SELECT a.name name1, b.name name2
FROM   naming_table A,
       naming_table B,
       mapping_table c
WHERE  A.m_id = c.m_id_1
AND    b.m_id = c.m_id_2;

答案 3 :(得分:0)

查询

with tab(M_ID, Name) as
(select 1,'apple' from dual union all
 select 2,'orange' from dual union all
 select 3,'pear' from dual),
tab2(M_ID_1, M_ID_2) as
(select 1,2 from dual union all 
 select 3,1 from dual union all 
 select 2,3 from dual)
----
--End of data
----
select (select name from tab where m_id = M_ID_1) M_ID_1, 
       (select name from tab where m_id = M_ID_2) M_ID_2
  from tab2;

输出:

| M_ID_1 | M_ID_2 |
|--------|--------|
|  apple | orange |
|   pear |  apple |
| orange |   pear |