首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

oracle中怎么查询出级联关系

2012-12-15 
oracle中如何查询出级联关系我现在有一个房屋表,有的房屋是属于某个建筑的,有的只是单纯的一座房屋。比如我

oracle中如何查询出级联关系
我现在有一个房屋表,有的房屋是属于某个建筑的,有的只是单纯的一座房屋。
比如我们经常住的电梯房,1栋楼有很多间房,而那种平房就没有这种概念。
房屋表是这样的:
houseid    housenumber        buildingcode   buildingname
   1           101                   1           **小区1栋
   2           102                   1           **小区1栋
   3           201                   1           **小区1栋
   4           101                   2           **小区2栋
   5           101                   2           **小区2栋

我最后想实现的是,查询出总共有多少这样的楼栋,然后通过楼栋找到它所属的房间。希望大侠能帮忙解决!
[最优解释]

--查询出总共有多少这样的楼栋
select count(distinct buildingcode) from 房屋表;

--通过楼栋找到它所属的房间,例如楼栋1下的房间
select housenumber from 房屋表 where buildingcode=1;

[其他解释]
--楼栋
select count(*) from (select buildingcode from 房屋表 group by buildingcode having count(*)>1)
--平房
select count(*) from (select buildingcode from 房屋表 group by buildingcode having count(*)=1)
[其他解释]
额,是这样的,就是有的房屋是单独的一个房屋,比如平房,那么它就不算楼栋,
我想是不是统计buildingcode的出现次数,如果是一次就不算楼栋,2次以上就证明是楼栋?
引用:
SQL code
--查询出总共有多少这样的楼栋
select count(distinct buildingcode) from 房屋表;

--通过楼栋找到它所属的房间,例如楼栋1下的房间
select housenumber from 房屋表 where buildingcode=1;

热点排行