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.
there’s nothing wrong with multiple tables in a case like this where you have multiple values. what you are doing here is fine. here’s the query you need:
it’s easy to write once you see how:
you could also use “join” explicitly, but i find the approach above simpler and it shouldn’t matter to the db engine.
[Edit from ypercube showing the JOIN syntax]:
[comment from ac: this and the initial syntax should be translated internally into the same query, so both are equally efficient]
[edit about performance] in general, the only (or at least, by far the most important) thing you need to worry about for good database performance is indices. you want to declare an index on the property_id column of every table, and also on the different type columns you have. that is critical. but once you have that, for just a few million rows, this should be fast – the above is not a very complex query and you have less than a GB of data (consider using tinyint for the type columns). don’t worry… and the aliases (the “as X”) are not an issue at all.
[edit for counts] for
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 = Xyou want something like:but i haven’t tested it or anything. that should give you multiple rows, with the location_type and the count for each row (so you do all the queries you gave above in one).