views:

49

answers:

3

I have a bind in the sql query (select * from users where name like '%?%') the bind set the ? Now, if i want to search with like method everything work but if, without change the sql, i want to search the exact match i dont now how to do.

I tried some regexp int the textbox es: _jon \jon\ [jon] and some others but nothing work properly.

Any Ideas?

Claudio

A: 

you can't search an exact match if the sql contains % symbols, as they are wildcards. you'll need to change the sql to

select * from users where name = '?'

for an exact match

(you can also use select * from users where name like '?' but that's more inefficient)

oedo
A: 

What is keeping you from changing the SQL?

The Like condition is for 'similar' matches, while the '=' is for exact matches.

Wadih M.
+2  A: 

Change your query to

select * from users where name like '?'

If you want to do a wildcard match, put the wildcards as part of the string that you're binding to the variable. If you don't want to do a wildcard match, then don't.

Note that like and = have the same performance except when your wildcard character is first in the string (for example, '%bob') as in that case the query optimizer can't use indexes as well to find the row(s) that you're looking for.

Donnie
Was about to type the same.
Alexander Pogrebnyak
re: like and = having the same performance : in mysql that's true, but OP doesn't mention which sql s/he is using...
oedo
@oedo : It's the same in pretty much every modern rdbms. It *used* to be different in a few, but thankfully optimizers have gotten smarter.
Donnie