如何让孩子计算父子关系

时间:2016-09-08 04:38:42

标签: mysql database

使用此查询将获得如下图像链接

所示的结果
select c.property_title,b.property_title as building,d.property_title as floor,e.house_name as house,
    f.room_no as room,g.bed_no as bed
    from g_property_group c
    left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
    left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
    left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
    left join g_room f on f.house_id = e.id and f.is_deleted = '0'
    left join g_bed g on  g.room_id = f.id and g.is_deleted = '0'
    where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
    and c.is_deleted = '0'

Click here to view image

Click here to see hierarchy

我已经写了一个查询来获取父母和子女的数量。请将建筑物数量作为7楼数统计为7号房屋数量为5个房间数量为4,而建筑物数量应为2,楼层= 2, house = 4,Rooms = 5和Bed 1。

select c.property_title,count(b.property_title)as building,count(d.property_title)as floor,count(e.house_name)as house,
count(f.room_no)as room,count(g.bed_no) as bed
from g_property_group c
left join g_property_group b on b.parent_id = c.id and b.is_deleted = '0'
left join g_property_group d on d.parent_id = b.id and d.is_deleted = '0'
left join g_house e on e.property_group_id = d.id and e.is_deleted = '0'
left join g_room f on f.house_id = e.id and f.is_deleted = '0'
left join g_bed g on g.house_id = g.id and g.room_id = f.id and g.is_deleted = '0'
where c.id = 'a976df373f75d3f8cc49938ae9fead8e4fc8ad19'
and c.is_deleted = '0'

提前谢谢

1 个答案:

答案 0 :(得分:0)

试试这个:

SQL SERVER

select count([building]) over (order by building) as [bulding count],
       count([floor]) over (order by [floor],[building]) as [floor count],
       count([house]) over (order by building,[floor],[house]) as [house count],
       count([room]) over (order by building,[floor],[house],[room]) as [room count],
       count([bed]) over (order by building,[floor],[house],[room],[bed]) as [room count]