I'm stumped on a SQL query and need your help. In plain English, I need to return all of the hotels which offer packages that contain ALL the items I select.
Tables involved are:
- hotels (hotel_id, hotel_name)
- packages (package_id, hotel_id, package_name)
- items (item_id, package_id, item_name)
- package_items (package_id, item_id)
SAMPLE DATA
To help explain my problem, let's look at this sample data to illustrate it more clearly.
Hotels table
---------------------- hotel_id | hotel_name ---------------------- 1 | Hilton 2 | Westin ----------------------
Packages table
----------------------------------------- package_id | hotel_id | package_name ----------------------------------------- 1 | 1 | Gold Package 2 | 1 | Silver Package 3 | 2 | Star Package 4 | 2 | Ultimate Package -----------------------------------------
Items table
------------------------------- item_id | item_name ------------------------------- 1 | Free Room Upgrade 2 | Dinner Included 3 | Spa Access 4 | Complimentary Papers -------------------------------
Package Items table
--------------------- package_id | item_id --------------------- 1 | 2 1 | 3 1 | 4 2 | 2 2 | 3 3 | 4 4 | 1 4 | 2 4 | 3 4 | 4 ---------------------
So, using this data you can see that for example, the Hilton's Gold Package contains Dinner Included, Spa Access, and Complimentary Papers.
I'm building a feature on our frontend that lets the user filter for these various package items. A check box against each item, which when clicked should filter the results to show only the hotels which offer packages that contains ALL of the items selected by the user.
I'm a little stumped on the SQL for this. So far my research has brought me to the following sample code (found here: PostgreSQL where all in array):
SELECT conversation_id FROM conversations_users
WHERE user_id IN (1,2)
GROUP BY conversation_id HAVING COUNT(*) = 2
In this sample query the key part of the query is the HAVING COUNT() = 2 section. With this, I expect to force the query to ensure that ALL selected items must be matched, not just one, which is the default behavior of the query without the HAVING COUNT() = 2 section.
Using this approach, and the above table structures I have adapted this code to suit our own requirement, including the various JOIN queries etc. In the below example I've selected Dinner Included, Spa Access, and Complimentary Papers. I expect to get back both the Hilton and the Westin as both of these hotels offer packages which include ALL of these items. In the case of the Hilton their Gold Package offers what I've selected, and in the case of the Westin their Ultimate Package offers what I've selected.
Remember, I'm trying to find hotels which offer packages that contain ALL the items I'm searching for.
Here's what I've got so far:
SELECT h.hotel_id, h.hotel_name FROM hotels h
JOIN packages p ON h.hotel_id = p.hotel_id
JOIN package_items pi ON p.package_id = pi.package_id
JOIN items i ON pi.item_id = i.item_id
WHERE i.item_id IN (2,3,4)
GROUP BY h.hotel_id HAVING COUNT (*) = 3;
Unfortunately I'm not getting accurate results here and I'm stumped as to how to fix it. Serious kudos to anyone who can solve this problem for me.