finding products that customers bought together

前端 未结 2 726
清歌不尽
清歌不尽 2021-01-01 04:52

I am using PHP and MySQL If I have the following two tables

orders
----
|id|
|--|
|1 |
|2 |
|3 |
|4 |
----

items
----
|order_id|sku|
|------------|
|   1            


        
相关标签:
2条回答
  • 2021-01-01 05:31

    Try the following:

    SELECT c.original_SKU, c.bought_with, count(*) as times_bought_together
    FROM (
      SELECT a.sku as original_SKU, b.sku as bought_with
      FROM items a
      INNER join items b
      ON a.order_id = b.order_id AND a.sku != b.sku) c
    GROUP BY c.original_SKU, c.bought_with
    
    0 讨论(0)
  • 2021-01-01 05:34

    Here is a flat solution without the subquery:

    SELECT a.sku AS original_SKU, b.sku AS bought_with, count(*) as times_bought_together
    FROM items AS a
    INNER JOIN items AS b ON a.order_id = b.order_id
    AND a.sku != b.sku
    GROUP BY a.sku
    
    0 讨论(0)
提交回复
热议问题