---
title: "Oracle数据库表嵌套查询问题"
author: "NickWar"
date: "2009-09-21T18:55:00Z"
tags: []
slug: "oracle"
draft: false
lastmod: "2012-04-20T20:55:52Z"
id: 1492385
url: "https://nickwar.iteye.com/blog/1492385"
---

目前有两张表

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;