Find similar objects that share the most tags

﹥>﹥吖頭↗ 提交于 2020-01-03 06:38:13

问题


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?


回答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.



来源:https://stackoverflow.com/questions/12875040/find-similar-objects-that-share-the-most-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!