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

有三个数据表:

         表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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值