mysql - FInd relationships within three products -
i using php , mysql
if have following tables
items ---- |order_id|sku| |------------| | 1 | | | 1 | b | | 1 | c | | 1 | d | | 1 | e | | 2 | b | | 2 | c | | 2 | d | | 3 | d | | 3 | e | | 3 | f | | 4 | | | 4 | b | | 4 | c | | 4 | f | --------------
i retrieve following info:
| original_sku | bought_with_a | bought_with_a | times_bought_together | |--------------|---------------|---------------|-----------------------| | | b | c | 2 | | b | | c | 2 | | b | c | d | 2 | | c | b | d | 2 | etc etc
i trying count number of times 3 products bought together. extension question: finding products customers bought together
you should follow same approach in solution 3-way combinations:
select i1.sku sku1, i2.sku sku2, i3.sku sku3, count(*) bought_together items i1 join items i2 on i1.order_id = i2.order_id , i1.sku <> i2.sku join items i3 on i1.order_id = i3.order_id , i3.sku not in (i1.sku, i2.sku) group i1.sku, i2.sku, i3.sku;
you should able readily figure out how extend more products, adding more joins.
Comments
Post a Comment