home | contact us
» mysql » Compare two columns in mysql

BLOG CATEGORY: 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`,'%')


Click Here to Contact Us about Compare two columns in mysql
 

Comments

2 Comments

2 Responses to “Compare two columns in mysql”

  1. Anon says:

    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.

  2. Anushree says:

    Does it work if columns belong to 2 different tables.If so can you please give an example for same?

    Thanks

Leave a Reply

rss icon