Exact match with sql like and the bind

亡梦爱人 提交于 2019-12-12 02:17:15

问题


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?


回答1:


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.




回答2:


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)




回答3:


What is keeping you from changing the SQL?

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



来源:https://stackoverflow.com/questions/2904401/exact-match-with-sql-like-and-the-bind

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!