这将有助于如果你告诉我们,你的表结构,这样我就可以更具体。
我假设你已经有了类似这样的结构:
Table item: (id, itemname)
1 item1
2 item2
3 item3
4 item4
5 item5
Table tag: (id, tagname)
1 cool
2 red
3 car
Table itemtag: (id, itemid, tagid)
1 1 2 (=item1, red)
2 2 1 (=item2, cool)
3 2 3 (=item2, car)
4 3 1 (=item3, cool)
5 3 2 (=item3, red)
6 3 3 (=item3, car)
7 4 3 (=item3, car)
8 5 3 (=item3, car)
一般我的做法是通过计算每个单独的标签开始了。
-- make a list of how often a tag was used:
select tagid, count(*) as `tagscore` from itemtag group by tagid
这显示了分配给该项目的每个标签的一行。
在我们的例子,这将是:
tag tagscore
1 2 (cool, 2x)
2 2 (red, 2x)
3 4 (car, 4x)
set @ItemOfInterest=2;
select
itemname,
sum(tagscore) as `totaltagscore`,
GROUP_CONCAT(tags) as `tags`
from
itemtag
join item on itemtag.itemid=item.id
join
/* join the query from above (scores per tag) */
(select tagid, count(*) as `tagscore` from itemtag group by tagid) as `TagScores`
on `TagScores`.tagid=itemtag.tagid
where
itemid<>@ItemOfInterest and
/* get the taglist of the current item */
tagid in (select distinct tagid from itemtag where [email protected])
group by
itemid
order by
2 desc
说明: 查询有2子查询: 一是从感兴趣的项目获得该列表的标签。我们只想和那些人一起工作。 其他子查询会为每个标签生成一个分数列表。
所以最终,数据库中的每个项目都有标签分数列表。这些分数加起来为sum(tagscore)
,这个数字用于排序结果(最高分)。
要显示可用标签的列表中,我使用GROUP_CONCAT。
查询将导致这样的事情(我做了实际的数据在这里):
Item TagsScore Tags
item3 15 red,cool,car
item4 7 red,car
item5 7 red
item1 5 car
item6 5 car
这两个回复都处于正确的轨道,并让我走向短期解决方案。就如何扩展这个例程而言,我仍在寻找! – seans 2009-09-03 23:00:03