I'm building a practice website that allows users to search hotels based off of the amenities that the hotel provides. From the FE, the user will select a checkbox for however many amenities they want, and the amenity keys will be sent to the backend. On the backend, I've got these three tables:
hotels
| id | name | vacancies
| ---| ------------ | -------- |
| 1| Marriott | 0 |
| 2| Best Western | 10 |
| 3| Sheraton | 3 |
------------------------------
amenities
id
name
key
1
Cafe
cafe
2
Wheelchair Accessible
wheelchair_accessible
3
Wifi
wifi
hotels_amenities_lookup
id
amenity_id
hotel_id
1
1
3
2
2
1
3
2
2
4
2
3
5
3
2
6
3
1
To search for one amenity, such as wheelchair_accessible, I would do something like this:
WITH hotels_with_amenity as (
SELECT ha.hotel_id
FROM hotels_amenities_lookup ha
JOIN (
SELECT id from amenities a
WHERE a.key = 'wheelchair_accessible'
) amenity ON ha.amenity_id = amenity.id
)
SELECT h.name,
h.vacancies
FROM hotels h, hotels_with_amenity hwa
WHERE h.id = hwa.hotel_id;
Returns all three hotels.
The question is: if the user selects multiple amenities, wheelchair_accessible and wifi for example, how would I query for hotels that have both? With this current set up, I couldn't do
WHERE a.key = 'wheelchair_accessible AND a.key = 'wifi'
Is there a better way of setting up these tables to make this query easier?
I'm new to relational databases and it's likely I'm missing something obvious here.