从三个关联的数据表中按照分类的方式读取数据
2004-11-10 18:28
549 查看
有三个数据表:
表1 表2 表3
ID(主键) ID(主键) ID(主键)
Animal tab1ID(表1主键) tab2ID(表2主键)
Type Name
怎样用一条sql语句实现以下读取方式:
Animal1
Type1
Name1
Name2
Name3
Type2
Name4
Type3
Name5
Name6
Animal2
Type4
Name7
Name8
Name9
Type5
Name10
Name11
Name12
Type6
Name13
Name14
Name15
Name16
Name17
...
...
...
解决方法:
select oid, name, type from (select a.oid||b.oid||c.oid col, ' '||c.itemname name, c.oid oid, '1' type from lz_law a,lz_lawchapter b,lz_lawitem c where a.oid=b.lawoid and b.oid=c.chapteroid union all select a.oid||b.oid col, ' '||b.chapname name, b.oid oid, '2' type from lz_law a,lz_lawchapter b where a.oid=b.lawoid union all select a.oid col,a.lawname name, a.oid oid, '3' type from lz_law a ) t order by col
注意:在此SQL语句中有两处空格字符串加字段值,以用于缩进显示;如果是在B/S中使用,此处的空格应该为汉字的全角空格。
在此要感谢:bzszp(SongZip)
表1 表2 表3
ID(主键) ID(主键) ID(主键)
Animal tab1ID(表1主键) tab2ID(表2主键)
Type Name
怎样用一条sql语句实现以下读取方式:
Animal1
Type1
Name1
Name2
Name3
Type2
Name4
Type3
Name5
Name6
Animal2
Type4
Name7
Name8
Name9
Type5
Name10
Name11
Name12
Type6
Name13
Name14
Name15
Name16
Name17
...
...
...
解决方法:
select oid, name, type from (select a.oid||b.oid||c.oid col, ' '||c.itemname name, c.oid oid, '1' type from lz_law a,lz_lawchapter b,lz_lawitem c where a.oid=b.lawoid and b.oid=c.chapteroid union all select a.oid||b.oid col, ' '||b.chapname name, b.oid oid, '2' type from lz_law a,lz_lawchapter b where a.oid=b.lawoid union all select a.oid col,a.lawname name, a.oid oid, '3' type from lz_law a ) t order by col
注意:在此SQL语句中有两处空格字符串加字段值,以用于缩进显示;如果是在B/S中使用,此处的空格应该为汉字的全角空格。
在此要感谢:bzszp(SongZip)
相关文章推荐
- SQL Server 语句操纵数据库
- SQL Server 索引结构及其使用(一)--深入浅出理解索引结构第1/4页
- c#接简单数据库操作类
- oracle中的分页
- 備份服務器端SQL SERVER數據庫至本地目錄
- Ant应用(3)
- 解决打过震荡波补丁之后oracle无法正常启动的问题
- SymbianOS Series 60学习笔记三:数据库初探
- Oracle9i中一个特殊等待事件--null event
- log4j使用示例
- SymbianOS Series 60学习笔记三:数据库初探
- 在线人数的统计方法。
- 一般网站用户注册流程:(适用初学者)
- 如何使用DELPHI和要SQL SERVER数据库连接
- 轻松搞定数据访问层2
- 数据库的Query Analyzer
- oracle Performance analysis
- 什么是中间件
- SYBASE 数据库迁移到AS 400 db2的FAQ(四)
- only a sql