本文介绍了查找客户一起购买的产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用PHP和MySQL如果我有以下两个表
I am using PHP and MySQLIf I have the following two tables
orders
----
|id|
|--|
|1 |
|2 |
|3 |
|4 |
----
items
----
|order_id|sku|
|------------|
| 1 | A |
| 1 | B |
| 1 | C |
| 2 | B |
| 2 | A |
| 3 | A |
| 4 | B |
| 4 | C |
--------------
我想获取以下信息:
| original_SKU | bought_with | times_bought_together |
|--------------|-------------|-----------------------|
| A | B | 2 |
| A | C | 1 |
| B | A | 2 |
| B | C | 2 |
| C | A | 1 |
| C | B | 2 |
------------------------------------------------------
我不知道从哪里开始.干杯
I cant think where to begin with this. Cheers
推荐答案
尝试以下操作:
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
这篇关于查找客户一起购买的产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!