MySQL regex query case insensitive

后端 未结 4 1642
星月不相逢
星月不相逢 2021-02-14 16:56

In my table I have firstname and last name. Few names are upper case ( ABRAHAM ), few names are lower case (abraham), few names are character starting with ucword (Abraham).

相关标签:
4条回答
  • 2021-02-14 17:06

    I think you should query your database making sure that the names are lowered, suppose that name is the name you whish to find out, and in your application you've lowered it like 'abraham', now your query should be like this:

    SELECT * FROM `test_tbl` WHERE LOWER(cus_name) = name
    

    Since i dont know what language you use, I've just placed name, but make sure that this is lowered and you should retrieve Abraham, ABRAHAM or any variation of the name!

    Hepe it helps!

    0 讨论(0)
  • 2021-02-14 17:11

    Have you tried:

    SELECT * FROM `test_tbl` WHERE LOWER(cus_name) REGEXP '^[abc]';
    
    0 讨论(0)
  • 2021-02-14 17:17

    You don't need regexp to search for names starting with a specific string or character.

    SELECT * FROM `test_tbl` WHERE cus_name LIKE 'abc%' ; 
    

    % is wildcard char. The search is case insensitive unless you set the binary attribute for column cus_name or you use the binary operator

    SELECT * FROM `test_tbl` WHERE BINARY cus_name LIKE 'abc%' ; 
    
    0 讨论(0)
  • 2021-02-14 17:19

    A few valid options already presented, but here's one more with just regex:

    SELECT * FROM `test_tbl` WHERE cus_name REGEXP '^[abcABC]';
    
    0 讨论(0)
提交回复
热议问题