Question : Searching for multiple values

I have a search form that basically has individual values that the end user can select that will search the database for those individual values; however, I need to add a search type that has multiple values.  I'm not sure how to do this type of search.

For example, I have a record with a bunch of values and one of the items is location.  Each record could have been assigned multiple locations.
   ID            Data1     Data2    Data3     Data4     Location Name from other table
Record1    Data1     Data2    Data3     Data4     Orlando
                                                                            Dallas
Record2    Data1     Data2    Data3     Data4     Orlando
Record3    Data1     Data2    Data3     Data4     Harrisburg
                                                                            Dallas

Now the location data is in a separate table...so that table looks like this:
strLocationCode            LocationName
Record1                           Orlando
Record1                            Dallas
Record2                           Orlando
Record3                           Harrisburg
Record3                           Dallas

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
<cfform name="Search" action="search.cfm?reportID=1" format="flash" skin="haloblue" width="300" height="500" timeout="500">
<cfinput type="Text"
     name="ID" 
     label="ID Number:">
<cfselect NAME="Location"
	query="get_LocationList"
	display="LocationName"
	value="strLocationCode"
	multiple="yes"
	size="11"
         width="233" 
	label="Affected Location(s):"
</cfselect>
</cfform>

<!--- the query to get the results from the search form --->
<CFQUERY datasource="myDNS" name="getmySearchResults">
SELECT    BLAH BLAH BLAH
FROM      BLAH BLAH BLAH
WHERE    ID = ID 
	<CFIF Len(Trim(form.ID))>
	         AND ID LIKE <CFQUERYPARAM CFSQLTYPE="CF_SQL_VARCHAR" VALUE="#form.ID#">
	</CFIF>
<!--- HOW DO I WRITE THE WHERE STATEMENT IN ORDER TO GET THE RECORDS BASED ON THE LOCATION OR LOCATIONS THE END USER SELECTED ON THE FORM??? --->
	<CFIF Len(Trim(form.Location))>
	         AND ??????????????????????????????
	</CFIF>
</CFQUERY>

Answer : Searching for multiple values

I'm not sure about the structure of the search table either. But if each location is stored as a separate record, I'd use a simple JOIN and IN (...) clause.  It's not clear whether it should be an INNER or OUTER join, but ...

<cfparam name="FORM.Location" default="">

<cfquery....>
SELECT   Columns
FROM      SearchTableName sr INNER JOIN LocationTableName loc ON
                        sr.LocationName = loc.LocationName
WHERE  ID = ID
.....
<cfif listLen(form.location)>
      AND loc.strLocationCode  IN
     (
      <cfqueryparam value="#form.location#" list="true" cfsqltype="(your data type)">          
      )
</cfif>

If the locations ARE are stored as comma delimited list (less than ideal), then you'd probably have to resort to looping and the old LIKE hack

Random Solutions  
 
programming4us programming4us