Our blog

 

Compare two columns in mysql

Many database queries rely on seeing if two columns match / differ, however sometimes you may want to check if either column contains a sub-string of the other. MySql allows you to do this, but it isn’t that well documented, so hopefully this will help someone looking for this in the future


SELECT *
FROM
`table`
WHERE
`col1` LIKE CONCAT('%', `col2`, '%')
OR col2 LIKE CONCAT('%',`col1`,'%')

More Reading:

One Comments

Anon
February 11th, 2012

I just had to write this to thank you!

This is EXACTLY what I needed. I have been combing Google for an answer for hours now. Yours is exactly what I was looking for.

 

 

Leave a Reply