Just wondering if this is possible to do in MySQL:
I have two search fields where users can search for sizes&quantities, for example:
S=2, M=2, L=2, XL=4
Right now I'm doing a big construct in Coldfusion to pass up to 10 sizes/qty pairs into a stored procedure to spit out a string of matching articles, which I'm feeding into the actual search.
Since this is a key-search function on the site I'm working I'm wondering if it is possible to do this straight in MySQL, that is find products with matching sizes, quantities and matching size-quantity pairs like so:
sizes: S|M|L|XL
= = = =
qtys: 2|2|2|4
This is what I'm doing right now:
<cfif L.s_lot_groesse neq "" AND L.s_lot_menge neq "" AND ListLen(L.s_lot_groesse,",") EQ ListLen(L.s_lot_menge,",")>
<cfif StructKeyExists(Session, "filter") AND Session.Filter NEQ 0>
<cfscript>
variables.lot_laenge = ListLen(L.s_lot_groesse,",");
</cfscript>
</cfif>
// qtys
<cfstoredproc procedure="proc_select_lots" datasource="db">
<cfprocparam type="in" value="#trim(GetToken(L.s_lot_menge, 1, ","))#" cfsqltype="cf_sql_integer">
<cfprocparam type="in" value="#trim(GetToken(L.s_lot_menge, 2, ","))#" cfsqltype="cf_sql_integer">
<cfprocparam type="in" value="#trim(GetToken(L.s_lot_menge, 3, ","))#" cfsqltype="cf_sql_integer">
....
// sizes
<cfprocparam type="in" value="#trim(GetToken(L.s_lot_groesse, 1, ","))#" cfsqltype="cf_sql_varchar" maxlength="35">
<cfprocparam type="in" value="#trim(GetToken(L.s_lot_groesse, 2, ","))#" cfsqltype="cf_sql_varchar" maxlength="35">
<cfprocparam type="in" value="#trim(GetToken(L.s_lot_groesse, 3, ","))#" cfsqltype="cf_sql_varchar" maxlength="35">
....
<cfprocparam type="in" value="#variables.lot_laenge#" cfsqltype="cf_sql_integer">
<cfprocresult name="lot_articles">
</cfstoredproc>
<cfif lot_articles.recordcount GT 0>
<cfloop list="#lot_articles#" delimiters=", " index="arts">
<cfset variables.lotbedingung = variables.lotbedingung & arts & '|'>;
</cfloop>
</cfif>
And in MySQL, I'm doing:
SELECT art.artikelnummer, count(*) as anzahl
FROM articles AS art
WHERE 1
AND IF( param_filter = '', 1, art.iln = param_filter )
AND IF( size_1 = '', 1, ( art.groesse = size_1 AND art.bestand >= qty_1 ) )
AND IF( size_2 = '', 1, ( art.groesse = size_2 AND art.bestand >= qty_2 ) )
...
While this works, I'm wondering if this can be done in an easier way?
Thanks for inputs!