這將有助於如果你告訴我們,你的表結構,這樣我就可以更具體。
我假設你已經有了類似這樣的結構:
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