Paul B. Paul B. - 1 month ago 7
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

this will find common tags providing 2 names:

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