I am having trouble with drilling down some data, this is the table structure
Products Table
uid_product (unique id)
txt_prod_name (product name)
Table Name: tbl_products
Product Features
uid_feature(unique id)
txt_feature_name(feature name)Table Name: tbl_features
Assigned Product Features
uid_prodf (unique id)
uid_prodf_prodid (foreign key to uid_product)
uid_prodf_featid(foreign key to the uid_features)
Table Name: tbl_prod_features
One product can have many features hence the use of the tbl_prod_features table.
I have created the following query, to obtain the results;
<cfquery name="getFeatureProducts" datasource="#application.dsn#">
SELECT uid_product, txt_prod_name
FROM dbo.tbl_products
WHERE uid_product IN (SELECT (dbo.tbl_product_features.uid_prodf_prodid)
FROM dbo.tbl_product_features
WHERE uid_prodf_featid IN (<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.uid_features#" list="yes">))
ORDER BY dbo.tbl_products.txt_prod_name
</cfquery>
This works great for getting all products associated to the features selected by the end user. However what I need to achieve is the following,
If a user selects;
Feature A = 5 products displayed (3 out of 5 of these products contains Feature B)
If the user then selects;
Feature B = 3 Products
Currently with the query using the in operator it will still retrieve 5 products.
My first thoughts was to create a dynamic where statement adding the AND operator, however uid_prodf_featid = 5 AND uid_prodf_featid = 6 will always produce 0 results.
My second idea was to build an array of product id’s from the first drill down i.e. get the id’s of the first 5 products, and pass them in to the query and then drill down the features, this works and the correct results are obtained.
However the user has the ability to remove features (and in any order), so I would somehow need to keep track of the products id’s as and when they were added and removed.
I am sure I am overcomplicating this, but any help would be appreciated.
Use a
HAVINGclause to count the number of matching features and return only products that have all of the requested features.For example, if
arguments.uid_featuresalways contains unique feature id’s, you could do something like this: