PDA

View Full Version : MYSQL query



jimjiminyjimjim
June 10th, 2009, 04:39 PM
I would like to match two words in a query but the two word might switch order e.g

"SELECT * FROM products WHERE class LIKE '%".$search."%' ORDER BY class, sliderpos ";

"product" might be for example silver trays

but search might be "trays silver"

Whats the correct query as I don't think my current one is always matching.

Much appreciated.

biznuge
June 10th, 2009, 05:21 PM
why not just use a conditional operator within the query?

á la...


SELECT * FROM products WHERE class LIKE '%".$search1."%' OR '%".$search2."%';

This is a very simplistic version though. I suppose should you want to do real multiple terms you could split the incoming search string on " " space, then get an array of terms, then build up your SQL query dynamically to include each of your terms within the OR condition.

This wouldn't be perfect I guess, as there'd be no real way to weight searches that had ALL of the terms and those that only have a single one, but this might go some way to solving your problem I guess.


Hope this helps anyhoo!