Search code examples
mysqlsqlsql-like

Is there any function for finding the result with special character in a sql like?


I'm try to get data from SQL using LIKE successful to get data but the result more than I need

Here is my some data

apple1
apple2
apple3
applejuice1
applej1
applej2

My query:

select * from apple where name like '%applej%'

Currently I got:

applejuice1
applej1
applej2

My Expected output:

applej1
applej2

Solution

  • Using REGEXP this is possbile:

    select * 
    from apple 
    where `name` REGEXP '^applej[0-9]'
    

    Demo on db<>fiddle.


    Update:

    If the name has data as applej1, applej2, applej15, applej109, the following query will work:

    select * 
    from apple 
    where `name` REGEXP '^applej[0-9]+'
    

    Demo on db<>fiddle