桌子有点像
collectionId | itemdId | user_id
-----------------------------------
123 | 2345 | 1
123 | 3465 | 1
123 | 876 | 1 // <---
123 | 567 | 1
123 | 980 | 1 // <---
777 | 980 | 2 // <---
777 | 332 | 2
777 | 3465 | 2
777 | 876 | 2 // <---
777 | 678 | 2
777 | 567 | 2
... ... ...
etc..
您会看到项目876和980都包含在两个系列(777和123)中,因此很受欢迎
所以说我访问了项目876的页面
我想向我的用户显示一个非常常见的项目,它与之关联/合并在一起,是项目980(当然这是基于用户的品味)
Think for a moment what Amazon does, if you see a white iphone i want
to suggest you a pink iphone cover because many other users have
suggested/favorited that together with the white iphone
for total number of collection:
select all item from collection 1
select all item from collection 2
do array_interesct (c1,c2)
store the matching items
repeat...
select all item from collection 2
do array_interesct (c1,c3)
store the matching items
repeat...
...then elect all item from collection 2 and repeat all the iterations..
但我想知道这是否只能通过MysqL来完成
解决方法:
SELECT collectionId
FROM wishLists
WHERE itemId = 876
由此,您想要获得那些集合中的所有其他itemIds.
SELECT itemId
FROM wishLists
WHERE collectionId IN (above query)
AND itemId != 876
可以将其重写为联接:
SELECT a.itemId
FROM wishLists AS a
JOIN wishLists AS b ON a.collectionId = b.collectionId
WHERE a.itemId != 876 AND b.itemId = 876
SELECT a.itemId
FROM wishLists AS a
JOIN wishLists AS b ON a.collectionId = b.collectionId
WHERE a.itemId != 876 AND b.itemId = 876
GROUP BY a.itemId
ORDER BY COUNT(*) DESC