MySQL - 查找某些日期可用的酒店

时间:2016-07-04 07:01:13

标签: mysql

我读过很多类似的问题但却无法让自己发挥作用。我正在创建一个酒店预订系统,我希望列出在要求的预订日期内至少有一个房间的酒店。

这是我的表格结构 Table Structure

这是我到目前为止的查询

function cleanData(&$str)
    {
        $str = preg_replace("/\t/", "\\t", $str);
        $str = preg_replace("/\r?\n/", "\\n", $str);
        if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"';
    }
$flag = false;
foreach($data as $row1) {
    if(!$flag) {
        // display field/column names as first row
        echo implode("\t", array_keys($row1)) . "\r\n";
        $flag = true;
        }
    array_walk($row, __NAMESPACE__ . '\cleanData');
    echo implode("\t", array_values($row1)) . "\r\n";
   }
$filename = "users_data" . date('Ymd') . ".xls";

header("Content-Type: application/xls");
header("Content-Disposition: attachment; filename=\"$filename\"");

我是MySQL新手,这真的失控了。我们的想法是计算酒店的总房数,并减去给定日期范围内预订的房间总数。我可以得到一些帮助吗?

子查询

SELECT
  ac.id,
  ac.name,
  ac.link,
  ac.type,
  ac.country,
  co.name AS countryName,
  ac.state,
  st.statename,
  ac.cityid AS cityId,
  ci.name AS cityName,
  ac.addr,
  ac.featuredimage,
  ac.amenities,
  ac.starrating,
  ac.bookings_received,
  ro.roomprice,
  SUM(ro.numberofrooms) AS totalRooms,
  roomsBooked = (
  SELECT
    *,
    SUM(numberofroomsbooked) AS roomsBooked
  FROM
    bookings
  WHERE
    reservationto >= '2016-07-07' AND reservationfrom <= '2016-07-09'
    AND hotelid = ro.hotel
)
FROM
  accomodations ac
JOIN countries co ON ac.country = co.id
JOIN states st ON ac.state = st.id
JOIN city ci ON ci.id = ac.cityid
JOIN room ro ON ac.id = ro.hotel
WHERE
  ac.active = 1 AND ac.delete = 0
GROUP BY
  ac.id

完全适用于它本身,但不适用于此。

2 个答案:

答案 0 :(得分:1)

修改:基于之前的查询

SELECT
  ac.id,
  ac.name,
  ac.link,
  ac.type,
  ac.country,
  co.name AS countryName,
  ac.state,
  st.statename,
  ac.cityid AS cityId,
  ci.name AS cityName,
  ac.addr,
  ac.featuredimage,
  ac.amenities,
  ac.starrating,
  ac.bookings_received,
  ro.roomprice,
  ro.totalRooms
FROM
  accomodations ac
JOIN countries co ON ac.country = co.id
JOIN states st ON ac.state = st.id
JOIN city ci ON ci.id = ac.cityid
JOIN (
  SELECT
    hotel,
    roomprice,
    SUM(numberofrooms) AS totalRooms
  FROM
    room
  GROUP BY
    hotel
) ro ON ac.id = ro.hotel
JOIN (
  SELECT
    hotelid,
    SUM(numberofroomsbooked) ASroomsBooked
  FROM
    bookings
  WHERE
    reservationto >= '2016-07-07' AND reservationfrom <= '2016-07-09'
  GROUP BY
    hotelid
) bo ON ac.id = bo.hotelid
WHERE
  ac.active = 1 AND ac.delete = 0
GROUP BY
  ac.id

更新后的查询已删除内部选择并将其添加为内部联接

SELECT
  ac.id,
  ac.name,
  ac.link,
  ac.type,
  ac.country,
  co.name AS countryName,
  ac.state,
  st.statename,
  ac.cityid AS cityId,
  ci.name AS cityName,
  ac.addr,
  ac.featuredimage,
  ac.amenities,
  ac.starrating,
  ac.bookings_received,
  ro.roomprice,
  SUM(ro.numberofrooms) AS totalRooms,
  SUM(tm.roomsBooked ) as roomsBooked,
FROM
  accomodations ac
JOIN countries co ON ac.country = co.id
JOIN states st ON ac.state = st.id
JOIN city ci ON ci.id = ac.cityid
JOIN room ro ON ac.id = ro.hotel
(
  SELECT
    hotelid,
    SUM(numberofroomsbooked) AS roomsBooked
  FROM
    bookings
  WHERE
    reservationto >= '2016-07-07' AND reservationfrom <= '2016-07-09'
    group by hotelid
) tm on tm.hotelid=ro.hotel
WHERE
  ac.active = 1 AND ac.delete = 0
GROUP BY
  ac.id

答案 1 :(得分:0)

您是否尝试过此查询?您正在按住宿酒店进行分组,因此,如果您使用功能SUM与预订的房间,它应该返回您预订的房间总数。我没试过。

我希望它能解决你的问题。

http://api.openweathermap.org/data/2.5/weatherq=London,uk&appid=d7b900681c37193223281142bd919019