我有3张桌子:
table A: building_id, region_code
table B: building_id, storey_no
table C: building_id, storey_no, room_no
什么查询可以计算每个区域中所有建筑物的房间数量总和,以及特定范围内的楼层数量(以下是不起作用的样本查询,因为我不知道如何包含表格B里面有楼层数等条件)?
SELECT SUM(C.room_no) AS room_sum, A.building_id
FROM A,B,C
WHERE A.region_code=1
AND A.building_id=C.building_id
AND B.storey_no>3
AND B.storey_no<10
GROUP BY C.building_id,C.room_no
答案 0 :(得分:1)
尝试从以下三个表中加入building_id:
SELECT SUM(C.room_no) AS room_sum, C.building_id
FROM A INNER JOIN B
ON A.building_id = B.building_id
INNER JOIN C
ON A.building_id = C.building_id
WHERE A.region_code=1
AND B.storey_no>3
AND B.storey_no<10
GROUP BY C.building_id