MySQL “LIKE” search doesn't work

馋奶兔 提交于 2019-12-31 06:42:23

问题


I've imported a .txt database in MySQL through "LOAD DATA INFILE", and everything seemed working, the only problem is that if I search a record on the DB with the following query:

 "SELECT * FROM hobby WHERE name LIKE  'Beading'"

it returns 0 rows, while if I use

""SELECT * FROM hobby WHERE name LIKE  '%Beading%'"

it returns 1 row, even if exist one record with name=Beading. Does anybody know what could it depend on?


回答1:


When using sql LIKE:

You should use a wildcard identifier such as (or not):

  1. '%word' - return results that the value ends with the letters: "word".
  2. 'word%' - reurn results that begin with the letters: "word".
  3. %word% - return results that include the letters: "word" anywhere.
  4. there are some more pattern search combination like:

    'abc' LIKE 'abc' true

    'abc' LIKE 'a%' true

    'abc' LIKE 'b' true

    'abc' LIKE 'c' false

If you want an exact match you should use:

"SELECT * FROM hobby WHERE name='Beading'"

But LIKE 'Beading' should work also, so its probably a spaces issue or case sensitivity porblem.

You need to take care of collation case (sensitivity) when you want to make sure your results are complete.

Say your table is UTF...CS and you want to make an insensitive case search you should declare it in your sql query, for example:

SELECT * FROM hobby WHERE name COLLATE UTF8_GENERAL_CI LIKE 'beading'

try testing different approaches and see what fits your goal best.




回答2:


the first one doesnt work because Like statment you should use '%search word%'

you should use this one

      "SELECT * FROM hobby WHERE name LIKE  '% Beading %' or name LIKE '% Beading'
       or name LIKE 'Beading %' or name LIKE 'Beading'"


来源:https://stackoverflow.com/questions/17777704/mysql-like-search-doesnt-work

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