sql query to determine the most similar goods by tags

前端 未结 3 2099
余生分开走
余生分开走 2021-02-11 07:48

i\'m making an e-store, so i have 3 tables:

1) goods

id      | title
--------+----------- 
1       | Toy car
2       | Toy pony
3       | Do         


        
3条回答
  •  名媛妹妹
    2021-02-11 08:33

    Some help:

    Assuming you are looking the most similar to goods#1

    SELECT a.*  
    FROM (SELECT * FROM goods WHERE id <> 1) a 
    LEFT JOIN (SELECT z.goods_id, count(*) as total
              FROM links z
              WHERE z.goods_id <> 1 AND
              z.tag_id in (SELECT DISTINCT tag_id from links where goods_id = 1)
              GROUP BY z.goods_id) b 
    ON a.id = b.goods_id
    ORDER by b.total DESC
    

    However, I think you can try something a bit different. Instead of ordering by the number of common tags, you can sort by the ratio of common tags. With this you will avoid the fact that the products with more tags will appear always at the top of the rankings, even if the relative common tags are not many.

提交回复
热议问题