我试图从查询中获得正确的结果,并且遇到了一些困难,我的产品通过它们的类别,子类别和关联的类别显示。相关类别是用户可以将产品与次级类别关联的位置(对于类似产品)。将嵌套的内部或子查询添加到现有查询中
数据正在由产品的功能向下钻取例如摆动或滑动等
的特征向下钻取应是,如果秋千被选择,只有产品所选择的类别,子类别内或与其相关联的类别的结果应该显示波动。
最终用户然后可以点击另一个特征,例如,幻灯片,并且结果应该只是其选定类别,子类别或带有波动和幻灯片的关联类别中的产品。
下面的查询得到我的产品与相关的类别(这工作正常)使用嵌套查询:
<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>
下面的查询是我用使用功能来深化产品:
<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>
目前上面的查询返回的所有产品没有考虑类别,子类别或关联的类别。我需要能够将第一个查询的功能添加到第二个查询中。我可以通过添加一个isdefined
和一个sql AND语句来完成类别和子类别的罚款;这是我正在努力的相关类别。不知道如何将嵌套查询添加到WHERE子句,仍然保持查询工作,我试着玩弄,但只是没有工作,因为它总是给出错误的结果。
任何帮助表示赞赏!
顺便说一句,它看起来像你在第一个查询中缺少一些括号。当“AND”/“OR”语句混合使用时,需要使用括号来确保评估和结果的正确顺序。 – Leigh 2012-01-30 14:52:05