简体   繁体   English

oracle的联接表

[英]joining table of oracle

Hi friends i am having problem in joining two tables in oracle my two tables are shown bellow 嗨,朋友们,我在Oracle中加入两个表时遇到问题,下面显示了两个表

table1 looks like table1看起来像

 id    Name    Jan  
 7001  Deven   22 
 7002  Clause  55 
 7004  Monish  11 
 7010  Dipesh  55
 7070  Dipika  100

table2 looks like table2看起来像

  id      Name       Feb  
  7001    Deven      12 
  7002    Clause     15 
  7003    Nimesh     20 
  7004    Monish     21 
  7005    Ritesh     22 

i want to combine this two table and want answer like bellow 我想结合这两个表,并希望像波纹管一样回答

table2 looks like table2看起来像

  id      Name      Jan   Feb  
  7001    Deven     22    12 
  7002    Clause    55    15 
  7003    Nimesh    -     20 
  7004    Monish    11    21 
  7005    Ritesh    -     22 
  7010    Dipesh    55    -
  7070    Dipika    100   -

We join asymmetrical records sets using the OUTER JOIN syntax. 我们使用OUTER JOIN语法联接非对称记录集。 A LEFT JOIN will provide the results you want: 左联接将提供您想要的结果:

SQL> select t72.id
  2         , t72.name
  3         , t34.jan
  4         , t72.feb
  5  from t72
  6       left outer join t34
  7       on ( t72.id = t34.id)
  8  order by t72.id
  9  /

        ID NAME                        JAN        FEB
---------- -------------------- ---------- ----------
      7001 Deven                        22         12
      7002 Clause                       55         15
      7003 Nimesh                                  20
      7004 Monish                       11         21
      7005 Ritesh                                  22

SQL>

edit 编辑

I note you have amended the sample data while I was knocking up the demo. 我注意到您在我删除演示时已经修改了示例数据。

When both tables have values of ID which the other table lacks we can use a FULL JOIN to retrieve values from both sides: 当两个表都具有另一个表所缺少的ID值时,我们可以使用FULL JOIN从两侧检索值:

SQL> select nvl(t72.id, t34.id) as id
  2         , nvl(t72.name, t34.name) as name
  3         , t34.jan
  4         , t72.feb
  5  from t72
  6       full outer join t34
  7       on ( t72.id = t34.id)
  8  order by t72.id
  9  /

        ID NAME                        JAN        FEB
---------- -------------------- ---------- ----------
      7001 Deven                        22         12
      7002 Clause                       55         15
      7003 Nimesh                                  20
      7004 Monish                       11         21
      7005 Ritesh                                  22
      7010 Dipesh                       55
      7070 Dipika                      100

7 rows selected.

SQL>

Based on the sample data given in the question, I think you have some records only exist in either table1 or table2. 根据问题中给出的样本数据,我认为您只有table1或table2中存在一些记录。 So inner join is not applicable. 因此inner join不适用。 Please use full outer join as follows, 请按如下方式使用full outer join

SELECT NVL(table1.id,table2.id), NVL2(table1.id,table1.name,table2.name), table1.jan, table2.feb 
FROM  table1
FULL OUTER JOIN table2 ON table1.id = table2.id

For more info about SQL Joins, refer Wikipedia article 有关SQL连接的更多信息,请参阅Wikipedia文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM