Oracle数据库表嵌套查询问题

来源:互联网 发布:ictclas java 编辑:程序博客网 时间:2024/05/16 01:42

目前有两张表

type

--------------------

   ID |   Name

--------------------

   1  |   林地

--------------------

   2  |   耕地

--------------------

 

ground

 

   ID |   oldtype  | nowtype

----------------------------------------

   1  |        1       |    2

----------------------------------------

   2  |        2       |    1

----------------------------------------

 

目标是生成以下视图

 

sv_GroundType

----------------------------------------

   ID |   oldtype |   nowtype

----------------------------------------

   1  |     林地     |      耕地  

----------------------------------------

   2  |     耕地     |      林地  

----------------------------------------


语句:

create or replace view sv_GroundType as
select oldid ID,oldname oldtype,nowname nowtype
from
(select a.id oldid,b.name oldtype from ground a,type b where a.oldtype=b.id),
(select c.id newid,d.name nowname from ground c,type d where a.nowtype=c.id)
where oldid = newid

 

不知道如何精确描述这样的问题,先这样记下来吧,希望能对看到的人有所帮助。

 

更高级点的写法:

 

SELECT
 Type_1.TypeName,
 Type.TypeName,
 Ground.ID,
 Ground.LYR_SEQ,
 Layer.LayerName,
FROM
 Layer
  RIGHT JOIN
   (((Ground
     INNER JOIN
      Ground AS Ground_1
     ON Ground.ID = Ground_1.ID)
    INNER JOIN Type
    ON Ground.NowType = Type.TypeID)
   INNER JOIN Type AS Type_1
   ON Ground_1.OldType = Type_1.TypeID)
  ON Layer.LYR_SEQ = Ground.LYR_SEQ;