MySQL “LIKE” search doesn't work

前端 未结 2 427
轻奢々
轻奢々 2021-01-26 13:30

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

相关标签:
2条回答
  • 2021-01-26 13:59

    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'"
    
    0 讨论(0)
  • 2021-01-26 14:01

    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%' - return 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 also work, so it's probably a spaces issue or case sensitivity problem.

    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.

    0 讨论(0)
提交回复
热议问题