0

I am using PHP with MySql. I need a query which should fetch me results according to my requirements.

I have a table property_features_table which has properties with some features.

In the front end I have a search functionality. When a user selects multiple features suppose balcony,wifi,2-bedroom etc., He should be shown with properties having ALL the features he selected.

But when I use the following code, I am getting results (properties) which has atleast one of the features.

$featuresString = implode("','",$features);
$featuresString = "'".$featuresString."'";
$query = " SELECT * FROM property_features_tbl WHERE property_features_tbl.feature_id in (".$featuresString.")";

$features is an array which contains user selected features.

I want to display properties which has all the features selected by the user. Help me in writing the query.

11
  • how you are storing features in database? Commented Dec 10, 2013 at 13:55
  • Please show us output of featuresString Commented Dec 10, 2013 at 13:58
  • @alok : the table has the following columns primary_key,property_id,feature_id, same property can have many features and same feature can have many properties. Commented Dec 10, 2013 at 14:00
  • @AshReva here is what I get with $featuresString '81','55','82','56','83' (second time) Commented Dec 10, 2013 at 14:02
  • @raghuveer999 did fired this query in database and checked? Commented Dec 10, 2013 at 14:03

1 Answer 1

1

Assuming you just want the property ids, then something like this:-

<?php

$featuresString = implode("','",$features);
$featuresString = "'".$featuresString."'";
$feature_count = count($features);
$query = " SELECT property_id, COUNT(*) AS feature_count 
        FROM property_features_tbl 
        WHERE property_features_tbl.feature_id in (".$featuresString.")
        GROUP BY property_id
        HAVING feature_count = $feature_count";

?>
5
  • I dont think this query will make any difference because he says his query is working in database Commented Dec 10, 2013 at 14:19
  • The query will work, but will bring back every row where it has such a feature. Seems he wants those where the property has all those features (ie, a property could have 20 features, and will only be returned in all the required features are in that 20) Commented Dec 10, 2013 at 14:24
  • @AshReva no, My query is not working in database as I wanted. It is showing me properties having atleast one of the features. I want properties having all the features. Commented Dec 10, 2013 at 14:25
  • @Kickstart, No doubt your query is working as per my requirement. But can you please explain me why my query didn't work and why did yours? Why COUNT(*) ? Why "GROUP BY property_id HAVING feature_count = $feature_count"? WHAT MADE THE DIFFERENCE? Commented Dec 11, 2013 at 7:18
  • 1
    You table would have multiple rows for a property, one for each feature. Your query was getting each of those rows where the feature was one you wanted. My version is finding all the rows for a property with the required features, using COUNT / GROUP BY to get a count of how many matching features each property has and then using HAVING to check that count is the same as the number of features you are looking for. Commented Dec 11, 2013 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.