How to search for rows containing a substring?

Well, you can always try WHERE textcolumn LIKE "%SUBSTRING%" – but this is guaranteed to be pretty slow, as your query can’t do an index match because you are looking for characters on the left side.

It depends on the field type – a textarea usually won’t be saved as VARCHAR, but rather as (a kind of) TEXT field, so you can use the MATCH AGAINST operator.

To get the columns that don’t match, simply put a NOT in front of the like: WHERE textcolumn NOT LIKE "%SUBSTRING%".

Whether the search is case-sensitive or not depends on how you stock the data, especially what COLLATION you use. By default, the search will be case-insensitive.

Updated answer to reflect question update:

I say that doing a WHERE field LIKE "%value%" is slower than WHERE field LIKE "value%" if the column field has an index, but this is still considerably faster than getting all values and having your application filter. Both scenario’s:

1/ If you do SELECT field FROM table WHERE field LIKE "%value%", MySQL will scan the entire table, and only send the fields containing “value”.

2/ If you do SELECT field FROM table and then have your application (in your case PHP) filter only the rows with “value” in it, MySQL will also scan the entire table, but send all the fields to PHP, which then has to do additional work. This is much slower than case #1.

Solution: Please do use the WHERE clause, and use EXPLAIN to see the performance.

Leave a Comment