以下查询似乎在计算“ratingValue”时存在问题,因为 SUM 中有 SUM(h.liked)。
SELECT d.itemID1 as item,
sum(d.sum + d.count*SUM(h.liked))/sum(d.count) as ratingValue
FROM history h, dev d
WHERE h.userID=:id_user
AND d.itemID1<>h.itemID
AND d.itemID2=h.itemID
GROUP BY d.itemID1,h.itemID
为了更好地理解,这是原始和有效的查询(来自 Slop One 算法):
我只是用“history”替换“rating”表,因为在我的例子中,r.ratingValue 是用户给 itemID 的所有“like”的总和(=> r.ratingValue = SELECT SUM(liked) FROM history GROUP通过 h.itemID ):
SELECT d.itemID1 as item,
sum(d.sum + d.count*r.ratingValue)/sum(d.count) as ratingValue
FROM rating r, dev d
WHERE r.userID=$userID
AND d.itemID1<>r.itemID
AND d.itemID2=r.itemID
GROUP BY d.itemID1