开发者

Refine sql result set by average

开发者 https://www.devze.com 2023-03-22 13:38 出处:网络
I have the following line of SQL that returns all fanart in the db that has a rating of 10... currently it works, but not as initially intended...

I have the following line of SQL that returns all fanart in the db that has a rating of 10... currently it works, but not as initially intended...

What I'd like is for it to return results where the average rating of a fanart is 10... each fanart row in the rating table can refer to the same itemid, allowing us to have multiple user ratings per single peice of artwork... i'd like it only return distinct itemid's whe开发者_JAVA百科re the average of all ratings is 10.

SELECT DISTINCT g.gametitle, 
                p.name, 
                g.id, 
                b.filename 
FROM   games AS g, 
       banners AS b, 
       platforms AS p, 
       ratings AS r 
WHERE  r.itemid = b.id 
       AND r.rating = '10' 
       AND g.id = b.keyvalue 
       AND r.itemtype = 'banner' 
       AND b.keytype = 'fanart' 
       AND g.platform = p.id 
ORDER  BY Rand() 
LIMIT  6; 

Here's hoping that someone smarter than me can figure this out!

I tried creating a sub-query that used the AVG() function but to no avail.

Looking forwards to some thought provoking replies!


This calls for AGGREGATION!

   SELECT g.gametitle, 
          p.name, 
          g.id, 
          b.filename 
   FROM   games AS g, 
          banners AS b, 
          platforms AS p, 
          ratings AS r 
   WHERE  r.itemid = b.id 
          AND g.id = b.keyvalue 
          AND r.itemtype = 'banner' 
          AND b.keytype = 'fanart' 
          AND g.platform = p.id 
   GROUP BY g.gametitle, 
            p.name, 
            g.id, 
            b.filename 
   HAVING AVG(r.rating) = 10
0

精彩评论

暂无评论...
验证码 换一张
取 消