Search code examples
mysqlregexselectsql-like

MySQL SELECT LIKE or REGEXP to match multiple words in one record


The field table.name contains 'Stylus Photo 2100' and with the following query

SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus 2100%'

I get no results. Of course i would if i searched

SELECT `name` FROM `table` WHERE `name` LIKE '%Photo 2100%'

How can I select the record by searching 'Stylus 2100' ?

Thanks


Solution

  • Well if you know the order of your words.. you can use:

    SELECT `name` FROM `table` WHERE `name` REGEXP 'Stylus.+2100'
    

    Also you can use:

    SELECT `name` FROM `table` WHERE `name` LIKE '%Stylus%' AND `name` LIKE '%2100%'