SQL like search string starts with

后端 未结 4 534
失恋的感觉
失恋的感觉 2020-12-29 17:52

Learning SQL. Have a simple table games with field title. I want to search based on title. If I have a game called Age of Empires III: Dynasties, and I use

相关标签:
4条回答
  • 2020-12-29 18:08

    Aside from using %, age of empires III to lower case is age of empires iii so your query should be:

    select *
    from games
    where lower(title) like 'age of empires iii%'
    
    0 讨论(0)
  • 2020-12-29 18:11

    You need to use the wildcard % :

    SELECT * from games WHERE (lower(title) LIKE 'age of empires III%');
    
    0 讨论(0)
  • 2020-12-29 18:15
    SELECT * from games WHERE (lower(title) LIKE 'age of empires III');
    

    The above query doesn't return any rows because you're looking for 'age of empires III' exact string which doesn't exists in any rows.

    So in order to match with this string with different string which has 'age of empires' as substring you need to use '%your string goes here%'

    More on mysql string comparision

    You need to try this

    SELECT * from games WHERE (lower(title) LIKE '%age of empires III%');
    

    In Like '%age of empires III%' this will search for any matching substring in your rows, and it will show in results.

    0 讨论(0)
  • 2020-12-29 18:27

    COLLATE UTF8_GENERAL_CI will work as ignore-case. USE:

    SELECT * from games WHERE title COLLATE UTF8_GENERAL_CI LIKE 'age of empires III%';
    

    or

    SELECT * from games WHERE LOWER(title) LIKE 'age of empires III%';
    
    0 讨论(0)
提交回复
热议问题