我现在有一个房屋表,有的房屋是属于某个建筑的,有的只是单纯的一座房屋。
比如我们经常住的电梯房,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栋我最后想实现的是,查询出总共有多少这样的楼栋,然后通过楼栋找到它所属的房间。希望大侠能帮忙解决!

解决方案 »

  1.   

    --查询出总共有多少这样的楼栋
    select count(distinct buildingcode) from 房屋表;--通过楼栋找到它所属的房间,例如楼栋1下的房间
    select housenumber from 房屋表 where buildingcode=1;
      

  2.   

    额,是这样的,就是有的房屋是单独的一个房屋,比如平房,那么它就不算楼栋,
    我想是不是统计buildingcode的出现次数,如果是一次就不算楼栋,2次以上就证明是楼栋?
      

  3.   

    --楼栋
    select count(*) from (select buildingcode from 房屋表 group by buildingcode having count(*)>1)
    --平房
    select count(*) from (select buildingcode from 房屋表 group by buildingcode having count(*)=1)