2011-12-14 70 views
2

似乎有類似的問題,但不完全。我試圖走下這條路(compare data sets and return best match),但發現自己難倒了。比較一組屬性以找到最佳匹配

我需要採取集並找到最佳匹配集。所以說我們有search_obj包含值(1,4,29,44,378,379)。我想找到具有相似值的其他對象,並理想地找到最符合此條件的對象。會有大量的其他物體,所以表演是一個大問題。

我目前在使用php和mysql,但如果它意味着更好的性能,我願意改變它。

謝謝你的幫助。

+0

您可能想要了解「相似」和「最佳匹配」的含義。 – goat 2011-12-14 04:38:09

回答

0

它只是來到我的腦海:

假設你有獨特的對(A,B)的表:

INSERT INTO t1 
VALUES (1,1), (1,2),    -- item to compare with 
     (2,1), (2,3),    -- has one common prop with 1 
     (3,1), (3,2),    -- has the same props as 1 
     (4,1), (4,2), (4,3), (4,4); -- has 2 same props with 1 

的:

CREATE table t1 (a INT, b INT, PRIMARY KEY (a, b)); 

現在你往裏面以下查詢將根據相似性排序其他項目:

SELECT t1.a, 
    COUNT(t2.a) as same_props_count, 
    ABS(COUNT(t2.a) - COUNT(*)) as diff_count 
FROM t1 
LEFT JOIN t1 as t2 ON t1.b = t2.b and t2.a = 1 
WHERE t1.a <> 1 
GROUP BY t1.a 
ORDER BY same_props_count DESC, diff_count; 


a, same_props_count, diff_count 
3, 2,    0 
4, 2,    2 
2, 1,    1 
+0

這會做豬...這樣做... 在所有的嚴肅性,謝謝你! – fraklo 2011-12-14 21:47:14

0

您可以使用array_intersect來計算兩個數組的交集,它返回第二個數組中存在的第一個數組的值。如果你正在比較多個列表,那麼你可以使用返回數組的長度(即長度越接近交叉點,因此,匹配越接近)。