2

Mysql database contains below kind of values :

'AE01-1056 Ricoh Aficio' OR 'Ricoh AE01-1087 (AE01-1069)' etc

AS if am a normal user i will search the product name in simple text like

AE011056 ... but the result is not found.

i hav tried this query:

$q="SELECT * FROM mytable WHERE (p.product_name LIKE '$name%' OR c.category_name LIKE '$name%' OR pm.name LIKE '$name%')";

What change should i make in my query to get the product , because i have tried LIKE operator & it's not working for me.

2 Answers2

3

Use replace function

$q="SELECT * FROM mytable 
WHERE (REPLACE(p.product_name,'-','') LIKE '%$name%' 
       OR REPLACE(c.category_name,'-','') LIKE '%$name%'
       OR REPLACE(pm.name ,'-','') LIKE '%$name%')";
hkutluay
  • 6,794
  • 2
  • 33
  • 53
0

I think there are only two ways:

1. Manipulate search string If you knwo, users are often search for a code and don't use nessesary hyphens, check the searchstring bevor searching if it follows a given format an insert the hypen if it is missing.

2. replace all hyphens in the where-statement see http://dev.mysql.com/doc/refman/5.7/en/string-functions.html#function_replace

Depending on your setup, solution one might be the more performant solution, since you only have to do one instead multiple stringmanipulations.

hubba
  • 21
  • 4