我試圖從查詢中獲得正確的結果,並且遇到了一些困難,我的產品通過它們的類別,子類別和關聯的類別顯示。相關類別是用戶可以將產品與次級類別關聯的位置(對於類似產品)。將嵌套的內部或子查詢添加到現有查詢中
數據正在由產品的功能向下鑽取例如擺動或滑動等
的特徵向下鑽取應是,如果鞦韆被選擇,只有產品所選擇的類別,子類別內或與其相關聯的類別的結果應該顯示波動。
最終用戶然後可以點擊另一個特徵,例如,幻燈片,並且結果應該只是其選定類別,子類別或帶有波動和幻燈片的關聯類別中的產品。
下面的查詢得到我的產品與相關的類別(這工作正常)使用嵌套查詢:
<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