-1

I have two tables objects and tags, each object having an id, and each tag having an id a name and a parent (the id of the object).

What I want to do is to choose an object then find other objects ordered by the amount of tags in common , e.g. to return the 5 most similar objects.

EDIT:

SELECT parent,COUNT(*) as count
FROM `tag` 
WHERE tag="house" OR tag="dog" OR tag="cat" 
GROUP BY parent 
ORDER BY count DESC

This one does what I want, and I could find the obejcts tags "house,dog,cat" with another query before this one. Any idea how I could combine these two queries?

2
  • 1
    I agree with @sachleen - would be good to see an attempt first. Giving things an honest try is a good way to get practice! Try setting one up at SQLFiddle? :)
    – halfer
    Oct 13, 2012 at 17:18
  • Sorry didn't have much to begin with, since I simply had no idea at all how to do that. I edited the question
    – Wurstbro
    Oct 13, 2012 at 17:35

1 Answer 1

1

Given one object, you can find its tags like this:

 SELECT t1.id
 FROM tags t1
 where t1.parent_id = ?

Building on that, you want to take that list of tags and find other parent_ids that share them.

 SELECT parent_id, count(*)
 FROM tags t2
 WHERE EXISTS (
     SELECT t1.id
     FROM tags t1
     WHERE t1.parent_id = ?
     AND t1.id = t2.id
 )
 GROUP BY parent_id

That will give you a count of how many tags those other parent_ids share.

You can ORDER BY count(*) desc if you'd like to find the "most similar" rows first.

Hope that helps.

1
  • I had to load the tags into my class anyways, so there was no need for the "combo" query. I had some more problems like not selecting the object itself etc . Won't test your solution but it looks fine, you shall be accepted ;)
    – Wurstbro
    Oct 13, 2012 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.