SQL 'LIKE' query using '%' where t

2019-01-23 01:36发布

I have an SQL query as below.

Select * from table 
where name like '%' + search_criteria + '%' 

If search_criteria = 'abc', it will return data containing xxxabcxxxx which is fine.

But if my search_criteria = 'abc%', it will still return data containing xxxabcxxx, which should not be the case.

How do I handle this situation?

8条回答
虎瘦雄心在
2楼-- · 2019-01-23 02:23

You need to escape it: on many databases this is done by preceding it with backslash, \%.

So abc becomes abc\%.

Your programming language will have a database-specific function to do this for you. For example, PHP has mysql_escape_string() for the MySQL database.

查看更多
该账号已被封号
3楼-- · 2019-01-23 02:25
Select * from table where name like search_criteria

if you are expecting the user to add their own wildcards...

查看更多
登录 后发表回答