views:

90

answers:

4

Hi guys,

I'm working on a faceted search in Coldfusion and SQL. I've tried creating a query like this:

SELECT * FROM Turbos 
WHERE
  PartNumber LIKE '%#trim(SearchCriteria)#%' 
  OR PartDescription LIKE '%#trim(SearchCriteria)#%' 
  AND (PumpingSpeed BETWEEN #minimum# AND #URL.speed#) 
  AND InletFlange LIKE '#URL.inlet#' 
  AND Bearing LIKE '#URL.bearing#' 
  AND Condition LIKE '#URL.condition#'

The problem is the server is returning rows that don't contain EVERY piece of data I'm supplying. How can I select ONLY those rows which contain all the criteria?

A: 

Just a guess really, but change

...'%#trim(SearchCriteria)#%' OR PartDescription ...

to

...'%#trim(SearchCriteria)#%' AND PartDescription ...

edit
or are you saying some have null values?

post comment edit
Imagine for example that InletFlange is empty for your part, and the user didn't put it in their search.
Then InletFlange LIKE '#URL.inlet#' will compare "" LIKE "", which is of course true, so the product shows up.

There are many ways to solve this. For example

  1. Default the search criteria to "N/A" or something
  2. Default the column in the database in a similar fashion.
  3. something along the lines of AND NOT ISNULL(InletFlange,'')=='' for each criteria
BioBuckyBall
If an empty string ('') is null, then yes, I do have null values.
Scott
no empty string is not null, but for your question, close enough. I will edit my answer...
BioBuckyBall
There are no empty fields in the database, that's what puzzles me; the items are getting pulled anyway.
Scott
+5  A: 

just wrap the OR bit in brackets:

(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...

at the moment you have A or B and C which is being read as A or (B and C). You want (A or B) and C.

And make sure you use cfqueryparam as suggested above.

Aidan Kane
Well, that worked... sort of. Now the search returns nothing, even when I specify all values.
Scott
Start isolating it down by removing the AND clauses one by one to see which is throwing away the results you expect to see. In particular I'd just leave the PartNumber and PartDescription in there to make sure it's matching on the Parts you'd expect.
Aidan Kane
@Aidan's suggestion is a good approach. BTW: Is there any chance your database is case sensitive (or you are using a QoQ)?
Leigh
Aiden: I don't think it's a particular clause that's the problem. (See my comment on the first reply.)Leigh: My database is an access database that will be migrated to mySQL shortly, so there's no case sensitivity.
Scott
Could you supply an example row you would expect returned along with the criteria you're using. Remember, without any conditions all data would be returned - you expect some data and you're getting none. The only sound conclusion is that one or more conditions are limiting rows that you expect to be in the result.
Aidan Kane
Also, just noticed that you're using LIKE on most conditions but don't actually have % in there anywhere. Maybe that's why you're nit seeing the results you expect.
Aidan Kane
Yup, that's exactly why! It works great now, thanks again! :)
Scott
@Scott - Interesting. I had mentioned the LIKE condition in another comment, but had not thought it would skew the results. Of course I do not normally use LIKE in that manner .. so that is good to know ;)
Leigh
A: 

Okay, I found the answers:

Leigh: +1 to clarifying the question. (It is unlikely this has any bearing on the results, but what is the point of using LIKE without wildcards "%"? If it is an equality comparison you are after, just use equals ie ... AND Condition = '#URL.condition#' )

You were quite right, I was missing the percent symbols in my LIKE comparisons (DUH!). I removed them earlier thinking they were the problem, but they weren't, this was:

Aiden: just wrap the OR bit in brackets:

(PartNumber LIKE '%#trim(SearchCriteria)#%' OR PartDescription LIKE '%#trim(SearchCriteria)#%') AND...

Thank you Aiden!

Scott
@Scott - Well .. that makes much more sense ;)
Leigh
A: 

While this isnt an answer to your question - you might want to consider using a dedicated search solution such as Solr (which is included with CF9). Its a more powerful fulltext solution than just raw SQL and includes facetting

Cody Caughlan