2016-07-04 8 views
0

Ich habe viele ähnliche Fragen gelesen, aber konnte es nicht für mich arbeiten lassen. Ich erstelle ein Hotelbuchungssystem und möchte Hotels auflisten, die mindestens ein Zimmer am gewünschten Buchungsdatum zur Verfügung haben.MySQL - Finden Sie Hotels, die an bestimmten Daten verfügbar sind

Hier ist meine Tabellenstruktur Table Structure

Und hier ist die Frage, die ich bisher habe

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 

Ich bin neu in MySQL und das ist wirklich aus der Hand. Die Idee besteht darin, die Gesamtzahl der Zimmer für ein Hotel zu berechnen und die Gesamtzahl der gebuchten Zimmer innerhalb eines bestimmten Datumsbereichs abzuziehen. Kann ich Hilfe bekommen?

Die Sub-Abfrage

 SELECT 
     *, 
     SUM(numberofroomsbooked) AS roomsBooked 
     FROM 
     bookings 
     WHERE 
     reservationto >= '2016-07-07' AND reservationfrom <= '2016-07-09' 
     AND hotelid = ro.hotel 

funktioniert perfekt auf die eigenen es aber nicht in dieser.

+0

Fügen Sie einige Beispieldaten hinzu und Ihr erwartetes Ergebnis ist hilfreicher. – Blank

+0

idem mit @ 10086 –

+0

Ich bin nicht in der Lage, meine Daten zu sqlfiddle hinzuzufügen, schlagen Sie anders vor? – VeeK

Antwort

1

Edit: Aufbauend auf der vorherigen Abfrage

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 

aktualisiert Abfrage Innen wählen entnommen und es als innere join

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 
+0

Danke Mahesh für deine Hilfe, du warst fast richtig. Ich habe Ihre Antwort so bearbeitet, dass sie die letzte Abfrage enthält, die korrekt mit meinen Daten funktioniert. Bitte akzeptieren Sie die Bearbeitung und ich werde Ihre als die richtige Antwort markieren – VeeK

+0

wow Great. genehmigt –

+0

bitte markiert dies als Antwort sonst t wird als unbeantwortete Frage angezeigt –

0

Haben Sie diese Abfrage versuchen? Sie gruppieren sich nach Unterkunftshotel, so dass Sie, wenn Sie die Funktion SUMME mit den gebuchten Zimmern nutzen, Ihnen die Summe der gebuchten Zimmer zurückgeben soll. Ich habe es nicht ausprobiert.

Ich hoffe, es löst Ihr Problem.

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(numberofroomsbooked) AS bookedRooms 
FROM 
    accomodations ac 
JOIN bookings b ON ac.id = b.hotelid 
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 
    AND reservationto >= '2016-07-07' AND reservationfrom <= '2016-07-09' 
GROUP BY 
    ac.id; 
Verwandte Themen