您的位置:首页 > 数据库

从三个关联的数据表中按照分类的方式读取数据

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  sql