0

以下查询似乎在计算“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
4

1 回答 1

2

正如错误消息所说,您不能使用下一个聚合函数。我想你的意思是:

SELECT d.itemID1 as item, 
       sum(d.sum + d.count*h.sumliked)/sum(d.count) as ratingValue
FROM (select h.userId, h.itemId, sum(h.liked) as sumliked
      from history h
      group by h.userId, h.itemId
     ) h join
     dev d 
     on h.userID=:id_user AND
        d.itemID1<>h.itemID AND
       d.itemID2=h.itemID 
GROUP BY d.itemID1

也就是说,您需要单独进行聚合,在这种情况下使用子查询。我还修复了查询中的连接语法。

于 2012-08-01T03:07:43.593 回答