2012-09-21 53 views
1

我的系統存儲來自許多不同電子商店的產品,我需要根據產品名稱配對產品。例如:MySQL產品配對

INPUT: MySQL表產品

id | name      | id_seller 
1 porsche 911 red edition  1 
2 red porsche 911 gt   2 
3 icecream      1 

期望的輸出:建議該產品1類似於產品2.

在第一步驟中,將足以使僅就常見詞語的數量提出建議 - 保時捷示例中的4分之三。

更復雜的解決方案將涉及比較單詞的順序,而不僅僅是他們的發生,但我想這不會是微不足道的。

只能使用MySQL查詢及其內置函數或任何複雜的庫/附加組件來完成嗎?

回答

2

這裏是一個SQLFiddle example找到對產品有至少一個共同的詞在name列:

select t.id id1, t.name name1, t1.id id2, t1.name name2 from t 
join t t1 
where t.id<t1.id 
     AND 
     t.name regexp 
     CONCAT('([[:<:]]', 
       REPLACE(
        TRIM(t1.name), 
        ' ', 
        '[[:>:]]|[[:<:]]' 
       ), 
       '[[:>:]])'); 

如果你需要找到至少N常用字線,你應該創建每個tmp目錄表拆分排成詞。 Here is an example and stored procedure to do it。對於示例此表的樣子:

id | name      
1 porsche 
1 911 
1 red 
1 edition  
2 red 
2 porsche 
2 911 
2 gt   
3 icecream      
在這種情況下

你可以用下面的查詢來查找與至少N常用詞ID(在這種情況下,N = 3):

select t1.id,t2.id,count(*) 
from tmp t1,tmp t2 
where t1.id<t2.id and t1.name=t2.name 
group by t1.id,t2.id having count(*)>=3 
+0

t1和t2是一樣的嗎? –

+0

是的,他們是上述TMP表的別名。看看從。我們在這裏加入TMP表格。 – valex

+0

哦,我的意思是t1和t2在第二個查詢中(當查找N個常用單詞時) –