MySQL - Using a Column Name Inside the LIKE Keyword

When using the LIKE keyword in a MySQL query, I use it the most typical way, LIKE '%STRING%'. One day, I was in need to use a column name instead and could not figure out how to do it! At first, I tried to just replace the string value with the column name like this, LIKE (%t.column%). The end-result was not good as the LIKE keyword expects a string.

So, I thought of trying the CONCAT() function since that returns a string. And it worked!

 LIKE CONCAT('%', t.column) 

Hope this helps someone!

4 comments for MySQL - Using a Column Name Inside the LIKE Keyword

Fred's picture

This was so useful!...

This was so useful!

Vivek's picture

great Solution !!...

great Solution !!

K2's picture

It helped me to quickly solve...

It helped me to quickly solve the problem when string variable that I was putting into the query from the search-field had the value 'And1' (company name). Thanks a lot!

Vivek's picture

Was realy great full...

Was realy great full Solution!!!

Many Thanks,
Vivek

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.