I am trying to get the correct result from a query and am having some difficulty, my products are shown via their category, sub-category and associated categories. Associated categories are where the user can associate products with a secondary category (for similar products).
The data is being drilled down by product features e.g swings or slides etc.
The result of a features drill down should be that if Swings is selected, only products within the selected category, subcategory or associated category with Swings should be displayed.
The end user can then click on another feature, e.g. Slides, and the results should be only products within its selected category, subcategory or associated category with swings AND slides.
The following query gets me products with associated categories (this works fine) using a nested query:
<cfquery name="getProductList" datasource="#application.dsn#">
SELECT
p.uid_product,
p.txt_prod_name,
p.uid_prod_templteid,
p.txt_prod_metaurl,
p.txt_prod_h1,
p.txt_prod_ref,
p.mon_prod_rrp,
p.mon_prod_current,
i.txt_pimg_name,
i.uid_pimages,
i.txt_pimg_alt
FROM dbo.tbl_products
IINNER JOIN tbl_product_images i ON (p.uid_product = i.uid_pimg_prodid)
WHERE p.uid_prod_webid=<cfqueryparam cfsqltype="cf_sql_integer" value="#application.webid#">
<!---If! If arguments to uid_subcategory is defined: USAGE! Get sub cats--->
<cfif Isdefined('arguments.uid_subcategory') AND arguments.uid_subcategory NEQ "">
AND p.uid_prod_subcatid = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.uid_subcategory#"></cfif>
AND dbo.tbl_product_images.bit_pimg_primary=<cfqueryparam cfsqltype="cf_sql_bit" value="yes">
<cfif Isdefined('arguments.uid_categories') AND arguments.uid_categories NEQ "">
AND p.uid_prod_catid = <cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.uid_categories#"> OR
<!---QUERY! Look for associated categories and deliver product to the page--->
uid_product IN (SELECT (ac.uid_assoc_prodid)
FROM tbl_assoc_categories ac
WHERE ac.uid_assoc_catid=<cfqueryparam cfsqltype="cf_sql_integer" value="#uid_categories#">
<!---If! If the user selects a subcategory we only want the associated products directly linked to the chosen sub category --->
<cfif Isdefined('arguments.uid_subcategory') AND arguments.uid_subcategory NEQ "">
AND ac.uid_assoc_subcatid=<cfqueryparam cfsqltype="cf_sql_integer" value="#arguments.uid_subcategory#"></cfif>)</cfif>
ORDER BY txt_prod_name asc
</cfquery>
<cfreturn getProductList>
</cffunction>
The following query is what i use to drill down the products using features:
<cfquery name="getFeatureProducts" datasource="#application.dsn#">
SELECT
p.uid_product,
p.txt_prod_name,
p.uid_prod_templteid,
p.txt_prod_metaurl,
p.txt_prod_h1,
p.txt_prod_ref,
p.mon_prod_rrp,
p.mon_prod_current,
i.txt_pimg_name,
i.uid_pimages,
i.txt_pimg_alt
FROM tbl_products p
INNER JOIN tbl_product_images i ON (p.uid_product = i.uid_pimg_prodid)
INNER JOIN tbl_product_features f ON (p.uid_product = f.uid_prodf_prodid)
WHERE 0=0
AND f.uid_prodf_featid IN (<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#arguments.uid_features#" list="yes">)
GROUP BY p.uid_product, p.txt_prod_name, p.uid_product, p.txt_prod_name, p.uid_prod_templteid, p.txt_prod_metaurl, p.txt_prod_h1, p.txt_prod_ref,p.mon_prod_rrp,p.mon_prod_current,i.txt_pimg_name,i.uid_pimages,i.txt_pimg_alt
HAVING COUNT(f.uid_prodf_featid) = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#ListLen(arguments.uid_features)#">
ORDER BY p.txt_prod_name
</cfquery>
At the moment the query above returns all the products without taking into account categories, sub-categories or associated categories. I need to be able to add the functionality of the first query into the second query. I can do the category and subcategory fine just by adding an isdefined and an sql AND statement; it’s the associated categories I’m struggling with. Not sure how to add the nested query to the WHERE clause and still keep the query working, I have tried playing around with but just didn’t work as it always gives the wrong results.
Any help is appreciated!
I’ve setup an example query on my site, sqlfiddle.com, which demonstrates your question (and my answer): http://sqlfiddle.com/#!3/8d051/2
Here’s the meat of it, including the CF code:
This method uses joins to filter the result set, and the cfloop to do so once per feature. This should work.