简体   繁体   English

SQL子查询检索每个酒店的总费用

[英]SQL sub query to retrieve total cost for each hotel

I'm trying to get my head around sub queries and I'm certain I don't actually know what I'm doing. 我正在努力使自己了解子查询,并且确定我实际上不知道自己在做什么。 I've got some code that pulls up the hotel names and room price, but the prices are showing the absolute total room price for all hotels together, not each hotel seperately. 我有一些代码可以提取酒店名称和房价,但是价格显示的是所有酒店(而不是单独分配每个酒店)的绝对总房价。

SELECT hotelName, SUM(roomPrice) AS 'Room Price'

FROM hotel, room

GROUP BY hotelName

This code gives me this 这段代码给了我

酒店名称和个人总房费

2360 is the total room cost over every hotel, I just need to change it to show the cost of each hotels total rooms, individually. 2360是每个酒店的总房间费用,我只需要更改它以分别显示每个酒店的总房间费用。

EDIT: Added a image of the database relations 编辑:添加了数据库关系的图像

数据库

SELECT H.hotelName, SUM(R.roomPrice) as 'Room Price'
FROM hotel H
JOIN room R
ON H.hotelNo = R.hotelNo
GROUP BY H.hotelNo;
select  SUM(b.roomPrice) AS 'Room Price' from hotel.hotel a, hotem.room b where a.hotelNo=b.hotelNo 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM