简体   繁体   English

根据日期查询MySQL Hotel Room总费用

[英]MySQL Hotel Room total fee query based on dates

I need to query a small hotel reservation system and I need help with the a query to get the rooms available on a specific set of dates with the total fees for the room. 我需要查询一个小型的酒店预订系统,并且需要一个查询帮助才能获得在特定日期可用的房间以及该房间的总费用。

The table looks like this: 该表如下所示:

+--------+------------+-------+--------+-------+------------------+
| RoomId | Date       | ResID |  Rate  | Tax   | DailyCleaningFee |
+--------+------------+-------+--------+-------+------------------+
|    1   | 2019-01-01 |   0   | 100    | 0.130 |    30            |
+--------+------------+-------+--------+-------+------------------+

ResID determines if the room is booked on certain dates or not. ResID确定房间是否在某些日期预订。 0 being room available. 0正在提供房间。 I want to query all rooms available for 2 nights on specific cates, and it should also return Total rate for the room including cleaning fee and taxes resulting in a table like this: 我想查询所有在特定Cates上2夜可用的房间,它还应该返回该房间的总价,包括清洁费和税费,生成如下表:

+---------+------------+
| RoomId  | TotalRate |
+-------- +------------+

If I want to query my table for 2 nights, 2019-01-02 and 2019-01-03, it should also exclude records where Tax is null and/or if the rate is set to 0. What would my query look like? 如果我要查询我的表2019年1月2日和2019年1月3日两晚,它还应排除Tax为空和/或费率设置为0的记录。查询的结果如何?

Really grateful for any help I can get. 非常感谢我能提供的任何帮助。 Thank you! 谢谢!

Edit: I added a DB Fiddle link to an example of the table. 编辑:我添加了一个DB Fiddle链接到表的示例。

Your question is missing some information to answer it accurately. 您的问题缺少一些信息,无法准确回答。 But I think I can help you to understand what your query should look like to get the expected result. 但是我想我可以帮助您了解您的查询应如何获得期望的结果。 See below. 见下文。

SELECT DISTINCT
   t1.RoomId AS RoomId, 
   (((t1.Rate + t1.DailyCleaningFee) * (1.0 + t1.Tax)) + ((t2.Rate + t2.DailyCleaningFee) * (1.0 + t2.Tax))) AS TotalRate  -- total rate has been the sum of each day rate + tax
FROM (
    SELECT h.*
    FROM `Hotel` h
    WHERE h.Date = '2018-12-02'   -- your first date goes here
    AND h.ResID = 0 AND h.Tax IS NOT NULL AND h.Rate > 0
) t1
INNER JOIN (
    SELECT h.*
    FROM `Hotel` h
    WHERE h.Date = '2018-12-03'  -- your second date goes here
    AND h.ResID = 0 AND h.Tax IS NOT NULL AND h.Rate > 0
) t2 ON t1.RoomId = t2.RoomId

This works, because, for each date you are searching (here you mentioned for two days), you need to have an inner query, and then you need to join all together by RoomId to collect rooms available in all of provided dates. 之所以RoomId ,是因为对于您要搜索的每个日期(这里提到了两天),您都需要进行内部查询,然后需要通过RoomId将所有内容结合在一起以收集在所有提供的日期中可用的房间。 After then only you can calculate the final rate. 之后,只有您才能计算最终汇率。

You may have to modify tax calculation formula which you have not clearly mentioned. 您可能需要修改未明确提及的税收计算公式。


Edit: (2018-12-01) 编辑: (2018-12-01)

Above query valid only explicitly for two days. 以上查询仅在两天内明确有效。

To calculate total rate for any number of continuous days , you can use following query. 要计算任何连续天数的总费率,可以使用以下查询。

SELECT
    t1.RoomId AS RoomId,
    ROUND(SUM(t1.DailyRate), 2) AS TotalRate
FROM (
    SELECT
        h.RoomId,
        ((h.Rate + h.DailyCleaningFee) * (1.0 + h.Tax)) AS DailyRate
    FROM Hotel h
    WHERE
        h.ResID = 0 
        AND h.Tax IS NOT NULL 
        AND h.Rate > 0
        AND h.Date >= '2018-12-02' AND h.Date <= '2018-12-04'
) t1
GROUP BY
    t1.RoomId
HAVING
    COUNT(*) >= DATEDIFF('2018-12-04', '2018-12-02') + 1;  

Note that the dates are inclusive in both boundaries. 请注意,日期包含两个边界。 Replace hardcoded dates with your date parameters. 用您的日期参数替换硬编码的日期。 In HAVING clause we check whether there are continuous days available from a single room for all days, and only filters them. HAVING子句中,我们检查单个房间是否有连续几天可供使用,并且仅对其进行过滤。

According to @Strawberry it might not be the efficient way to store room charges in a kind of given table. 根据@Strawberry的说法,这不是将房费存储在给定表中的有效方法。 You may need to change the schema little bit to make it gain more performance. 您可能需要稍微更改架构以使其获得更高的性能。

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

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