Paul B. Paul B. - 1 year ago 50
MySQL Question

How to search for a shared column value between two rows?

I am using Node.js with the mysql module and trying to figure out how I can search for all instances in a table where two users share a similar tag. A quick demo of my table would be:

username | tag
jennifer | coffee
jennifer | pizza
jennifer | travel
joe | sushi
joe | bowling
joe | coffee
joe | travel
celine | singing
celine | pizza
celine | programming

So for example I would like to find what the tags are that joe and jennifer have in common which in this case would be two: coffee and travel. What is the best way to search for the tags they have in common? Any advice would be helpful. Thank you in advance!

Answer Source

this will find common tags providing 2 names:

FROM yourTable
WHERE username in ("jennifer", "joe")
HAVING count(*) > 1