I've a table "location" with the structure:
id | property_id | location_type
1 | 1 | 1
2 | 1 | 2
3 | 2 | 1
4 | 3 | 2
5 | 4 | 1
6 | 4 | 2
I've another table "amenities" with the structure:
id | property_id | amenity_type
1 | 1 | 1
2 | 1 | 3
3 | 2 | 2
4 | 3 | 4
5 | 4 | 1
6 | 4 | 3
I've another table "property" with the structure:
id | property_id | property_type
1 | 1 | 2
2 | 1 | 3
3 | 2 | 2
4 | 3 | 4
5 | 4 | 2
6 | 4 | 3
id - is the primary key of the respective table. property_id is the property ID of my database (foreign key). location_type is beach (value - 1), mountain (value - 2).
amenity_type is car (value - 1), bike (value - 2), football (value - 3).
property_type is villa (value - 2), house (value - 3)
Can you please help me in getting the SQL query to select the property_id with location_type = 1 AND location_type = 2 AND amenity_type = 1 AND property_type = 3 AND property_type = 1 i.e. a property has beach and mountains and car and villa and house.
This is just an example of a filter in my property search application. There can be n combinations for this. Please share a common logic which will join all these tables and be optimized to search for around a million records.
I also need count for all the conditions. Please share query for the same.
[edit for taking count]:
suppose I get the count of (property_id with location_type = 1 AND location_type = 2 AND amenity_type = 1 AND property_type = 3 AND property_type = 1) as 1500. I need to get the count with same condition and other property_type, location_type, amenity_type.
For example:
1) count of (property_id with location_type = 1 AND location_type = 2 AND amenity_type = 1 AND property_type = 3 AND property_type = 1) AND location_type = 3
2) count of (property_id with location_type = 1 AND location_type = 2 AND amenity_type = 1 AND property_type = 3 AND property_type = 1) AND location_type = 4
3) count of (property_id with location_type = 1 AND location_type = 2 AND amenity_type = 1 AND property_type = 3 AND property_type = 1) AND amenity_type = 2
4) count of (property_id with location_type = 1 AND location_type = 2 AND amenity_type = 1 AND property_type = 3 AND property_type = 1) AND amenity_type = 3
and so on. Its getting a big overhead for me. Please help. Also, please note that location_types, amenity_type, property_type are dynamic i.e. a user can add more location_type in master tables and I need to get the count for any more location_types.
int- it would boost the performance when having an index.