我正在进行Coldfusion8 / MySQL查询,我正在扫描数据库中的A-B对,例如:
S=2, M=2, L=2, XL=2
我正在尝试改进最初处理这个问题的脚本,这会将条目限制为4对,因为我想知道为什么它首先选择一个匹配对的所有记录然后使用< em> HAVING 仅选择记录,所有对匹配。
以下是原始查询,之后是我当前的版本:
<!--- placeholders --->
<cfparam name="s01" default="">
<cfparam name="s02" default="">
<cfparam name="s03" default="">
<cfparam name="s04" default="">
<cfparam name="q01" default="">
<cfparam name="q02" default="">
<cfparam name="q03" default="">
<cfparam name="q04" default="">
<!--- check length of user inputs --->
<cfset sizes = ListLen(s_lot_groesse,",")>
<cfset qtys = ListLen(s_lot_menge,",")>
<!--- populate placeholders --->
<cfif sizes gt 0><cfset s01 = trim(ListGetAt(s_lot_groesse, 1,","))></cfif>
<cfif sizes gt 1><cfset s02 = trim(ListGetAt(s_lot_groesse, 2,","))></cfif>
<cfif sizes gt 2><cfset s03 = trim(ListGetAt(s_lot_groesse, 3,","))></cfif>
<cfif sizes gt 3><cfset s4 = trim(ListGetAt(s_lot_groesse, 4,","))></cfif>
<cfif qtys gt 0><cfset q01 = trim(ListGetAt(s_lot_menge, 1,","))></cfif>
<cfif qtys gt 1><cfset q02 = trim(ListGetAt(s_lot_menge, 2,","))></cfif>
<cfif qtys gt 2><cfset q03 = trim(ListGetAt(s_lot_menge, 3,","))></cfif>
<cfif qtys gt 3><cfset q04 = trim(ListGetAt(s_lot_menge, 4,","))></cfif>
<!--- query --->
<cfquery datasource="db" name="lotsuche">
SELECT styleno, count(*) as total_styles
FROM styles
WHERE 1 = 1
AND (
<cfif s01 neq "" AND q01 neq "">(groesse = "#s01#" AND bestand >= "#q01#")</cfif>
<cfif s02 neq "" AND q02 neq "">OR (groesse = "#s02#" AND bestand >= "#q02#")</cfif>
<cfif s03 neq "" AND q03 neq "">OR (groesse = "#s03#" AND bestand >= "#q03#")</cfif>
<cfif s04 neq "" AND q04 neq "">OR (groesse = "#s04#" AND bestand >= "#q04#")</cfif>
)
GROUP BY styleno
HAVING total_styles= "#sizes#"
</cfquery>
新版本:
<!--- build a 2D array --->
<cfscript>
variables.lotArray = ArrayNew(2);
variables.sizeCounter = 1;
variables.qtyCounter = 1;
</cfscript>
<cfloop list="#LOCAL.Search.s_lot_groesse#" delimiters=", " item="size">
<cfscript>
variables.lotArray[variables.lotCounter][1] = size;
variables.lotCounter = variables.lotCounter + 1;
</cfscript>
</cfloop>
<cfloop list="#LOCAL.Search.s_lot_menge#" delimiters=", " item="qty">
<cfscript>
variables.lotArray[variables.qtyCounter][2] = qty;
variables.qtyCounter = variables.qtyCounter + 1;
</cfscript>
</cfloop>
<!--- get array length --->
<cfset variables.lotArrayLen = arrayLen(variables.lotArray)>
<!--- query --->
<cfquery datasource="ds" name="lotsuche">
SELECT art.styleNo, count(*) as total_styles
FROM styles AS art
WHERE 1 = 1
<cfloop from="1" to="#variables.lotArrayLen#" index="i">
AND ( art.groesse = <cfqueryparam cfsqltype="cfsql_varchar" value="#variables.lotArray[i][1]#">
AND art.bestand >= <cfqueryparam cfsqltype="cfsql_varchar" value="#variables.lotArray[i][2]#">
</cfloop>
GROUP BY art.styleno
<!--- HAVING anzahl = "#variables.lotArrayLen#" --->
</cfquery>
问题:
我不明白为什么在原始查询中使用 OR 和 HAVING ,因为这不会选择所有一个匹配对的记录vs只挑选所有配对匹配的记录?如果要扫描几百万个条目,哪种方法更好/更快?另外,像我一样重新考虑整个事情是否有意义?
感谢您的帮助!
答案 0 :(得分:1)
HAVING子句与聚合函数一起使用,OR用于测试查询中的多个条件。我不确定旧查询中having子句的用途。您是否彻底测试了新查询以确保获得预期结果?
查看这两个查询后,您的第二个查询将无法正常工作。这两个记录可能具有需要满足的多个条件,并且查询可能返回空记录集。
我认为你正在寻找更像这样的东西。
AND
<cfloop from="1" to="#variables.lotArrayLen#" index="i">
( OR (art.groesse = <cfqueryparam cfsqltype="cfsql_varchar" value="#variables.lotArray[i][1]#">
AND art.bestand >= <cfqueryparam cfsqltype="cfsql_varchar" value="#variables.lotArray[i][2]#"> ) )