sql query to determine the most similar goods by tags

前端 未结 3 2097
余生分开走
余生分开走 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:40

    When you want to show the goods with goods id = 2

    SELECT DISTINCT
      goods.*
    FROM
      goods
      LEFT JOIN links ON links.goods_id = goods.id
    WHERE links.tag_id IN (SELECT links.tag_id 
                           FROM links
                           WHERE links.goods_id = 2)
    

    when you did not include goods_id = 2

    SELECT DISTINCT
      goods.*
    FROM
      goods
      LEFT JOIN links ON links.goods_id = goods.id
    WHERE links.goods_id != 2 AND links.tag_id IN (SELECT links.tag_id 
                           FROM links
                           WHERE links.goods_id = 2)
    

    can see on http://sqlfiddle.com/#!2/0fb60/38

提交回复
热议问题