MySQL locate function

By | March 1, 2012

So everyone knows sql LIKE to search wildcard matches the criteria. How about in a situation that you need to return all the rows that has value is part of the criteria string.

eg. given a full URL http://www.abc.com/bl/abcdefg/, return all the rows with uri_part field that values are part of the URL string.

+------------+
|uri_part    |
+------------+
|/bl/        |
|/bl/abc/    |
|/ba/abc/    |
+------------+

Normally LIKE statement works on the other way around, so you need to use LOCATE function. So in this example would be:

SELECT uri_part FROM table WHERE locate(uri_part, 'http://www.abc.com/bl/abcdefg/');

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.